ldd.c 19 KB

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