ldd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * A small little ldd implementation for uClibc
  4. *
  5. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  6. *
  7. * Several functions in this file (specifically, elf_find_section_type(),
  8. * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
  9. * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
  10. * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC
  11. * (GPL2).
  12. *
  13. * Licensed under GPLv2 or later
  14. */
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <fcntl.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <stdint.h>
  21. #include <sys/mman.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include "bswap.h"
  27. #include "link.h"
  28. #include "dl-defs.h"
  29. /* makefile will include elf.h for us */
  30. #ifdef DMALLOC
  31. #include <dmalloc.h>
  32. #endif
  33. #if defined(__alpha__)
  34. #define MATCH_MACHINE(x) (x == EM_ALPHA)
  35. #define ELFCLASSM ELFCLASS64
  36. #endif
  37. #if defined(__arm__) || defined(__thumb__)
  38. #define MATCH_MACHINE(x) (x == EM_ARM)
  39. #define ELFCLASSM ELFCLASS32
  40. #endif
  41. #if defined(__avr32__)
  42. #define MATCH_MACHINE(x) (x == EM_AVR32)
  43. #define ELFCLASSM ELFCLASS32
  44. #endif
  45. #if defined(__s390__)
  46. #define MATCH_MACHINE(x) (x == EM_S390)
  47. #define ELFCLASSM ELFCLASS32
  48. #endif
  49. #if defined(__hppa__)
  50. #define MATCH_MACHINE(x) (x == EM_PARISC)
  51. #if defined(__LP64__)
  52. #define ELFCLASSM ELFCLASS64
  53. #else
  54. #define ELFCLASSM ELFCLASS32
  55. #endif
  56. #endif
  57. #if defined(__i386__)
  58. #ifndef EM_486
  59. #define MATCH_MACHINE(x) (x == EM_386)
  60. #else
  61. #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
  62. #endif
  63. #define ELFCLASSM ELFCLASS32
  64. #endif
  65. #if defined(__ia64__)
  66. #define MATCH_MACHINE(x) (x == EM_IA_64)
  67. #define ELFCLASSM ELFCLASS64
  68. #endif
  69. #if defined(__mc68000__)
  70. #define MATCH_MACHINE(x) (x == EM_68K)
  71. #define ELFCLASSM ELFCLASS32
  72. #endif
  73. #if defined(__mips__)
  74. #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
  75. #define ELFCLASSM ELFCLASS32
  76. #endif
  77. #if defined(__powerpc64__)
  78. #define MATCH_MACHINE(x) (x == EM_PPC64)
  79. #define ELFCLASSM ELFCLASS64
  80. #elif defined(__powerpc__)
  81. #define MATCH_MACHINE(x) (x == EM_PPC)
  82. #define ELFCLASSM ELFCLASS32
  83. #endif
  84. #if defined(__sh__)
  85. #define MATCH_MACHINE(x) (x == EM_SH)
  86. #define ELFCLASSM ELFCLASS32
  87. #endif
  88. #if defined(__v850e__)
  89. #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
  90. #define ELFCLASSM ELFCLASS32
  91. #endif
  92. #if defined(__sparc__)
  93. #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
  94. #define ELFCLASSM ELFCLASS32
  95. #endif
  96. #if defined(__cris__)
  97. #define MATCH_MACHINE(x) (x == EM_CRIS)
  98. #define ELFCLASSM ELFCLASS32
  99. #endif
  100. #if defined(__x86_64__)
  101. #define MATCH_MACHINE(x) (x == EM_X86_64)
  102. #define ELFCLASSM ELFCLASS64
  103. #endif
  104. #ifndef MATCH_MACHINE
  105. # ifdef __linux__
  106. # include <asm/elf.h>
  107. # endif
  108. # ifdef ELF_ARCH
  109. # define MATCH_MACHINE(x) (x == ELF_ARCH)
  110. # endif
  111. # ifdef ELF_CLASS
  112. # define ELFCLASSM ELF_CLASS
  113. # endif
  114. #endif
  115. #ifndef MATCH_MACHINE
  116. # warning "You really should add a MATCH_MACHINE() macro for your architecture"
  117. #endif
  118. #if __BYTE_ORDER == __LITTLE_ENDIAN
  119. #define ELFDATAM ELFDATA2LSB
  120. #elif __BYTE_ORDER == __BIG_ENDIAN
  121. #define ELFDATAM ELFDATA2MSB
  122. #endif
  123. #ifndef UCLIBC_RUNTIME_PREFIX
  124. # define UCLIBC_RUNTIME_PREFIX "/"
  125. #endif
  126. struct library {
  127. char *name;
  128. int resolved;
  129. char *path;
  130. struct library *next;
  131. };
  132. struct library *lib_list = NULL;
  133. char not_found[] = "not found";
  134. char *interp_name = NULL;
  135. char *interp_dir = NULL;
  136. static int byteswap;
  137. static int interpreter_already_found = 0;
  138. static inline uint32_t byteswap32_to_host(uint32_t value)
  139. {
  140. if (byteswap == 1) {
  141. return (bswap_32(value));
  142. } else {
  143. return (value);
  144. }
  145. }
  146. static inline uint64_t byteswap64_to_host(uint64_t value)
  147. {
  148. if (byteswap == 1) {
  149. return (bswap_64(value));
  150. } else {
  151. return (value);
  152. }
  153. }
  154. #if ELFCLASSM == ELFCLASS32
  155. # define byteswap_to_host(x) byteswap32_to_host(x)
  156. #else
  157. # define byteswap_to_host(x) byteswap64_to_host(x)
  158. #endif
  159. static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
  160. {
  161. int j;
  162. ElfW(Shdr) *shdr;
  163. shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
  164. for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
  165. if (key == byteswap32_to_host(shdr->sh_type)) {
  166. return shdr;
  167. }
  168. }
  169. return NULL;
  170. }
  171. static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
  172. {
  173. int j;
  174. ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
  175. for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
  176. if (type == byteswap32_to_host(phdr->p_type)) {
  177. return phdr;
  178. }
  179. }
  180. return NULL;
  181. }
  182. /* Returns value if return_val==1, ptr otherwise */
  183. static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
  184. ElfW(Ehdr) *ehdr, int return_val)
  185. {
  186. ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
  187. unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
  188. for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
  189. if (key == byteswap_to_host(dynp->d_tag)) {
  190. if (return_val == 1)
  191. return (void *)byteswap_to_host(dynp->d_un.d_val);
  192. else
  193. return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
  194. }
  195. }
  196. return NULL;
  197. }
  198. static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
  199. {
  200. ElfW(Dyn) *dyns;
  201. for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
  202. if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
  203. char *strtab;
  204. strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  205. return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
  206. }
  207. }
  208. return NULL;
  209. }
  210. int check_elf_header(ElfW(Ehdr) *const ehdr)
  211. {
  212. if (!ehdr || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 ||
  213. ehdr->e_ident[EI_CLASS] != ELFCLASSM ||
  214. ehdr->e_ident[EI_VERSION] != EV_CURRENT)
  215. {
  216. return 1;
  217. }
  218. /* Check if the target endianness matches the host's endianness */
  219. byteswap = 0;
  220. #if __BYTE_ORDER == __LITTLE_ENDIAN
  221. if (ehdr->e_ident[5] == ELFDATA2MSB) {
  222. /* Ick -- we will have to byte-swap everything */
  223. byteswap = 1;
  224. }
  225. #elif __BYTE_ORDER == __BIG_ENDIAN
  226. if (ehdr->e_ident[5] == ELFDATA2LSB) {
  227. /* Ick -- we will have to byte-swap everything */
  228. byteswap = 1;
  229. }
  230. #else
  231. #error Unknown host byte order!
  232. #endif
  233. /* Be vary lazy, and only byteswap the stuff we use */
  234. if (byteswap == 1) {
  235. ehdr->e_type = bswap_16(ehdr->e_type);
  236. ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
  237. ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
  238. ehdr->e_phnum = bswap_16(ehdr->e_phnum);
  239. ehdr->e_shnum = bswap_16(ehdr->e_shnum);
  240. }
  241. return 0;
  242. }
  243. #ifdef __LDSO_CACHE_SUPPORT__
  244. static caddr_t cache_addr = NULL;
  245. static size_t cache_size = 0;
  246. int map_cache(void)
  247. {
  248. int fd;
  249. struct stat st;
  250. header_t *header;
  251. libentry_t *libent;
  252. int i, strtabsize;
  253. if (cache_addr == (caddr_t) - 1)
  254. return -1;
  255. else if (cache_addr != NULL)
  256. return 0;
  257. if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
  258. fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
  259. cache_addr = (caddr_t) - 1; /* so we won't try again */
  260. return -1;
  261. }
  262. cache_size = st.st_size;
  263. cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
  264. close(fd);
  265. if (cache_addr == MAP_FAILED) {
  266. fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
  267. return -1;
  268. }
  269. header = (header_t *) cache_addr;
  270. if (cache_size < sizeof(header_t)
  271. || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
  272. || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
  273. || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
  274. || cache_addr[cache_size - 1] != '\0')
  275. {
  276. fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
  277. goto fail;
  278. }
  279. strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
  280. libent = (libentry_t *) & header[1];
  281. for (i = 0; i < header->nlibs; i++) {
  282. if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
  283. fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
  284. goto fail;
  285. }
  286. }
  287. return 0;
  288. fail:
  289. munmap(cache_addr, cache_size);
  290. cache_addr = (caddr_t) - 1;
  291. return -1;
  292. }
  293. int unmap_cache(void)
  294. {
  295. if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
  296. return -1;
  297. #if 1
  298. munmap(cache_addr, cache_size);
  299. cache_addr = NULL;
  300. #endif
  301. return 0;
  302. }
  303. #else
  304. static inline void map_cache(void)
  305. {
  306. }
  307. static inline void unmap_cache(void)
  308. {
  309. }
  310. #endif
  311. /* This function's behavior must exactly match that
  312. * in uClibc/ldso/ldso/dl-elf.c */
  313. static void search_for_named_library(char *name, char *result,
  314. const char *path_list)
  315. {
  316. int i, count = 1;
  317. char *path, *path_n;
  318. struct stat filestat;
  319. /* We need a writable copy of this string */
  320. path = strdup(path_list);
  321. if (!path) {
  322. fprintf(stderr, "Out of memory!\n");
  323. exit(EXIT_FAILURE);
  324. }
  325. /* Eliminate all double //s */
  326. path_n = path;
  327. while ((path_n = strstr(path_n, "//"))) {
  328. i = strlen(path_n);
  329. memmove(path_n, path_n + 1, i - 1);
  330. *(path_n + i - 1) = '\0';
  331. }
  332. /* Replace colons with zeros in path_list and count them */
  333. for (i = strlen(path); i > 0; i--) {
  334. if (path[i] == ':') {
  335. path[i] = 0;
  336. count++;
  337. }
  338. }
  339. path_n = path;
  340. for (i = 0; i < count; i++) {
  341. strcpy(result, path_n);
  342. strcat(result, "/");
  343. strcat(result, name);
  344. if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
  345. free(path);
  346. return;
  347. }
  348. path_n += (strlen(path_n) + 1);
  349. }
  350. free(path);
  351. *result = '\0';
  352. }
  353. void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_suid,
  354. struct library *lib)
  355. {
  356. char *buf;
  357. char *path;
  358. struct stat filestat;
  359. /* If this is a fully resolved name, our job is easy */
  360. if (stat(lib->name, &filestat) == 0) {
  361. lib->path = strdup(lib->name);
  362. return;
  363. }
  364. /* We need some elbow room here. Make some room... */
  365. buf = malloc(1024);
  366. if (!buf) {
  367. fprintf(stderr, "Out of memory!\n");
  368. exit(EXIT_FAILURE);
  369. }
  370. /* This function must match the behavior of _dl_load_shared_library
  371. * in readelflib1.c or things won't work out as expected... */
  372. /* The ABI specifies that RPATH is searched first, so do that now. */
  373. path = elf_find_rpath(ehdr, dynamic);
  374. if (path) {
  375. search_for_named_library(lib->name, buf, path);
  376. if (*buf != '\0') {
  377. lib->path = buf;
  378. return;
  379. }
  380. }
  381. /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
  382. * Since this app doesn't actually run an executable I will skip
  383. * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
  384. if (is_suid == 1)
  385. path = NULL;
  386. else
  387. path = getenv("LD_LIBRARY_PATH");
  388. if (path) {
  389. search_for_named_library(lib->name, buf, path);
  390. if (*buf != '\0') {
  391. lib->path = buf;
  392. return;
  393. }
  394. }
  395. #ifdef __LDSO_CACHE_SUPPORT__
  396. if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
  397. int i;
  398. header_t *header = (header_t *) cache_addr;
  399. libentry_t *libent = (libentry_t *) & header[1];
  400. char *strs = (char *)&libent[header->nlibs];
  401. for (i = 0; i < header->nlibs; i++) {
  402. if ((libent[i].flags == LIB_ELF ||
  403. libent[i].flags == LIB_ELF_LIBC0 ||
  404. libent[i].flags == LIB_ELF_LIBC5) &&
  405. strcmp(lib->name, strs + libent[i].sooffset) == 0)
  406. {
  407. lib->path = strdup(strs + libent[i].liboffset);
  408. return;
  409. }
  410. }
  411. }
  412. #endif
  413. /* Next look for libraries wherever the shared library
  414. * loader was installed -- this is usually where we
  415. * should find things... */
  416. if (interp_dir) {
  417. search_for_named_library(lib->name, buf, interp_dir);
  418. if (*buf != '\0') {
  419. lib->path = buf;
  420. return;
  421. }
  422. }
  423. /* Lastly, search the standard list of paths for the library.
  424. This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
  425. path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
  426. #ifndef __LDSO_CACHE_SUPPORT__
  427. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  428. #endif
  429. ;
  430. search_for_named_library(lib->name, buf, path);
  431. if (*buf != '\0') {
  432. lib->path = buf;
  433. } else {
  434. free(buf);
  435. lib->path = not_found;
  436. }
  437. }
  438. static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
  439. {
  440. char *tmp, *tmp1, *tmp2;
  441. struct library *cur, *newlib = lib_list;
  442. if (!s || !strlen(s))
  443. return 1;
  444. tmp = s;
  445. while (*tmp) {
  446. if (*tmp == '/')
  447. s = tmp + 1;
  448. tmp++;
  449. }
  450. /* We add ldso elsewhere */
  451. if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
  452. int len = strlen(interp_dir);
  453. if (strcmp(s, interp_name + 1 + len) == 0)
  454. return 1;
  455. }
  456. for (cur = lib_list; cur; cur = cur->next) {
  457. /* Check if this library is already in the list */
  458. tmp1 = tmp2 = cur->name;
  459. while (*tmp1) {
  460. if (*tmp1 == '/')
  461. tmp2 = tmp1 + 1;
  462. tmp1++;
  463. }
  464. if (strcmp(tmp2, s) == 0) {
  465. //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
  466. return 0;
  467. }
  468. }
  469. /* Ok, this lib needs to be added to the list */
  470. newlib = malloc(sizeof(struct library));
  471. if (!newlib)
  472. return 1;
  473. newlib->name = malloc(strlen(s) + 1);
  474. strcpy(newlib->name, s);
  475. newlib->resolved = 0;
  476. newlib->path = NULL;
  477. newlib->next = NULL;
  478. /* Now try and locate where this library might be living... */
  479. locate_library_file(ehdr, dynamic, is_setuid, newlib);
  480. //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
  481. if (!lib_list) {
  482. lib_list = newlib;
  483. } else {
  484. for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
  485. cur->next = newlib;
  486. }
  487. return 0;
  488. }
  489. static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
  490. {
  491. ElfW(Dyn) *dyns;
  492. for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
  493. if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
  494. char *strtab;
  495. strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  496. add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
  497. }
  498. }
  499. }
  500. static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
  501. {
  502. ElfW(Phdr) *phdr;
  503. if (interpreter_already_found == 1)
  504. return NULL;
  505. phdr = elf_find_phdr_type(PT_INTERP, ehdr);
  506. if (phdr) {
  507. struct library *cur, *newlib = NULL;
  508. char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
  509. char *tmp, *tmp1;
  510. interp_name = strdup(s);
  511. interp_dir = strdup(s);
  512. tmp = strrchr(interp_dir, '/');
  513. if (*tmp)
  514. *tmp = '\0';
  515. else {
  516. free(interp_dir);
  517. interp_dir = interp_name;
  518. }
  519. tmp1 = tmp = s;
  520. while (*tmp) {
  521. if (*tmp == '/')
  522. tmp1 = tmp + 1;
  523. tmp++;
  524. }
  525. for (cur = lib_list; cur; cur = cur->next) {
  526. /* Check if this library is already in the list */
  527. if (strcmp(cur->name, tmp1) == 0) {
  528. //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
  529. newlib = cur;
  530. free(newlib->name);
  531. if (newlib->path != not_found) {
  532. free(newlib->path);
  533. }
  534. newlib->name = NULL;
  535. newlib->path = NULL;
  536. return NULL;
  537. }
  538. }
  539. if (newlib == NULL)
  540. newlib = malloc(sizeof(struct library));
  541. if (!newlib)
  542. return NULL;
  543. newlib->name = malloc(strlen(s) + 1);
  544. strcpy(newlib->name, s);
  545. newlib->path = strdup(newlib->name);
  546. newlib->resolved = 1;
  547. newlib->next = NULL;
  548. #if 0
  549. //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
  550. if (!lib_list) {
  551. lib_list = newlib;
  552. } else {
  553. for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
  554. cur->next = newlib;
  555. }
  556. #endif
  557. interpreter_already_found = 1;
  558. return newlib;
  559. }
  560. return NULL;
  561. }
  562. /* map the .so, and locate interesting pieces */
  563. /*
  564. #warning "There may be two warnings here about vfork() clobbering, ignore them"
  565. */
  566. int find_dependancies(char *filename)
  567. {
  568. int is_suid = 0;
  569. FILE *thefile;
  570. struct library *interp;
  571. struct stat statbuf;
  572. ElfW(Ehdr) *ehdr = NULL;
  573. ElfW(Shdr) *dynsec = NULL;
  574. ElfW(Dyn) *dynamic = NULL;
  575. if (filename == not_found)
  576. return 0;
  577. if (!filename) {
  578. fprintf(stderr, "No filename specified.\n");
  579. return -1;
  580. }
  581. if (!(thefile = fopen(filename, "r"))) {
  582. perror(filename);
  583. return -1;
  584. }
  585. if (fstat(fileno(thefile), &statbuf) < 0) {
  586. perror(filename);
  587. fclose(thefile);
  588. return -1;
  589. }
  590. if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
  591. goto foo;
  592. if (!S_ISREG(statbuf.st_mode))
  593. goto foo;
  594. /* mmap the file to make reading stuff from it effortless */
  595. ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
  596. if (ehdr == MAP_FAILED) {
  597. fclose(thefile);
  598. fprintf(stderr, "Out of memory!\n");
  599. return -1;
  600. }
  601. foo:
  602. fclose(thefile);
  603. /* Check if this looks like a legit ELF file */
  604. if (check_elf_header(ehdr)) {
  605. fprintf(stderr, "%s: not an ELF file.\n", filename);
  606. return -1;
  607. }
  608. /* Check if this is the right kind of ELF file */
  609. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
  610. fprintf(stderr, "%s: not a dynamic executable\n", filename);
  611. return -1;
  612. }
  613. if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
  614. if (statbuf.st_mode & S_ISUID)
  615. is_suid = 1;
  616. if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  617. is_suid = 1;
  618. /* FIXME */
  619. if (is_suid)
  620. fprintf(stderr, "%s: is setuid\n", filename);
  621. }
  622. interpreter_already_found = 0;
  623. interp = find_elf_interpreter(ehdr);
  624. #ifdef __LDSO_LDD_SUPPORT__
  625. if (interp
  626. && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
  627. && ehdr->e_ident[EI_CLASS] == ELFCLASSM
  628. && ehdr->e_ident[EI_DATA] == ELFDATAM
  629. && ehdr->e_ident[EI_VERSION] == EV_CURRENT
  630. && MATCH_MACHINE(ehdr->e_machine))
  631. {
  632. struct stat statbuf;
  633. if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
  634. pid_t pid;
  635. int status;
  636. static const char *const environment[] = {
  637. "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
  638. "SHELL=/bin/sh",
  639. "LD_TRACE_LOADED_OBJECTS=1",
  640. NULL
  641. };
  642. if ((pid = vfork()) == 0) {
  643. /* Cool, it looks like we should be able to actually
  644. * run this puppy. Do so now... */
  645. execle(filename, filename, NULL, environment);
  646. _exit(0xdead);
  647. }
  648. /* Wait till it returns */
  649. waitpid(pid, &status, 0);
  650. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  651. return 1;
  652. }
  653. /* If the exec failed, we fall through to trying to find
  654. * all the needed libraries ourselves by rummaging about
  655. * in the ELF headers... */
  656. }
  657. }
  658. #endif
  659. dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
  660. if (dynsec) {
  661. dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
  662. find_needed_libraries(ehdr, dynamic, is_suid);
  663. }
  664. return 0;
  665. }
  666. int main(int argc, char **argv)
  667. {
  668. int multi = 0;
  669. int got_em_all = 1;
  670. char *filename = NULL;
  671. struct library *cur;
  672. if (argc < 2) {
  673. fprintf(stderr, "ldd: missing file arguments\n");
  674. fprintf(stderr, "Try `ldd --help' for more information.\n");
  675. exit(EXIT_FAILURE);
  676. }
  677. if (argc > 2)
  678. multi++;
  679. while (--argc > 0) {
  680. ++argv;
  681. if (strcmp(*argv, "--") == 0) {
  682. /* Ignore "--" */
  683. continue;
  684. }
  685. if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
  686. fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
  687. fprintf(stderr, "\t--help\t\tprint this help and exit\n");
  688. exit(EXIT_SUCCESS);
  689. }
  690. filename = *argv;
  691. if (!filename) {
  692. fprintf(stderr, "No filename specified.\n");
  693. exit(EXIT_FAILURE);
  694. }
  695. if (multi) {
  696. printf("%s:\n", *argv);
  697. }
  698. map_cache();
  699. if (find_dependancies(filename) != 0)
  700. continue;
  701. while (got_em_all) {
  702. got_em_all = 0;
  703. /* Keep walking the list till everybody is resolved */
  704. for (cur = lib_list; cur; cur = cur->next) {
  705. if (cur->resolved == 0 && cur->path) {
  706. got_em_all = 1;
  707. printf("checking sub-depends for '%s'\n", cur->path);
  708. find_dependancies(cur->path);
  709. cur->resolved = 1;
  710. }
  711. }
  712. }
  713. unmap_cache();
  714. /* Print the list */
  715. got_em_all = 0;
  716. for (cur = lib_list; cur; cur = cur->next) {
  717. got_em_all = 1;
  718. printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
  719. }
  720. if (interp_name && interpreter_already_found == 1)
  721. printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
  722. else
  723. printf("\tnot a dynamic executable\n");
  724. for (cur = lib_list; cur; cur = cur->next) {
  725. free(cur->name);
  726. cur->name = NULL;
  727. if (cur->path && cur->path != not_found) {
  728. free(cur->path);
  729. cur->path = NULL;
  730. }
  731. }
  732. lib_list = NULL;
  733. }
  734. return 0;
  735. }