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