ldd.c 19 KB

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