ldd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. struct library *lib_list = NULL;
  132. char not_found[] = "not found";
  133. char *interp_name = NULL;
  134. 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 || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 ||
  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. /* Ick -- we will have to byte-swap everything */
  222. byteswap = 1;
  223. }
  224. #elif __BYTE_ORDER == __BIG_ENDIAN
  225. if (ehdr->e_ident[5] == ELFDATA2LSB) {
  226. /* Ick -- we will have to byte-swap everything */
  227. byteswap = 1;
  228. }
  229. #else
  230. #error Unknown host byte order!
  231. #endif
  232. /* Be vary lazy, and only byteswap the stuff we use */
  233. if (byteswap == 1) {
  234. ehdr->e_type = bswap_16(ehdr->e_type);
  235. ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
  236. ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
  237. ehdr->e_phnum = bswap_16(ehdr->e_phnum);
  238. ehdr->e_shnum = bswap_16(ehdr->e_shnum);
  239. }
  240. return 0;
  241. }
  242. #ifdef __LDSO_CACHE_SUPPORT__
  243. static caddr_t cache_addr = NULL;
  244. static size_t cache_size = 0;
  245. int map_cache(void)
  246. {
  247. int fd;
  248. struct stat st;
  249. header_t *header;
  250. libentry_t *libent;
  251. int i, strtabsize;
  252. if (cache_addr == (caddr_t) - 1)
  253. return -1;
  254. else if (cache_addr != NULL)
  255. return 0;
  256. if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
  257. fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
  258. cache_addr = (caddr_t) - 1; /* so we won't try again */
  259. return -1;
  260. }
  261. cache_size = st.st_size;
  262. cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
  263. close(fd);
  264. if (cache_addr == MAP_FAILED) {
  265. fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
  266. return -1;
  267. }
  268. header = (header_t *) cache_addr;
  269. if (cache_size < sizeof(header_t)
  270. || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
  271. || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
  272. || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
  273. || cache_addr[cache_size - 1] != '\0')
  274. {
  275. fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
  276. goto fail;
  277. }
  278. strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
  279. libent = (libentry_t *) & header[1];
  280. for (i = 0; i < header->nlibs; i++) {
  281. if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
  282. fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
  283. goto fail;
  284. }
  285. }
  286. return 0;
  287. fail:
  288. munmap(cache_addr, cache_size);
  289. cache_addr = (caddr_t) - 1;
  290. return -1;
  291. }
  292. int unmap_cache(void)
  293. {
  294. if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
  295. return -1;
  296. #if 1
  297. munmap(cache_addr, cache_size);
  298. cache_addr = NULL;
  299. #endif
  300. return 0;
  301. }
  302. #else
  303. static __inline__ void map_cache(void)
  304. {
  305. }
  306. static __inline__ void unmap_cache(void)
  307. {
  308. }
  309. #endif
  310. /* This function's behavior must exactly match that
  311. * in uClibc/ldso/ldso/dl-elf.c */
  312. static void search_for_named_library(char *name, char *result,
  313. const char *path_list)
  314. {
  315. int i, count = 1;
  316. char *path, *path_n;
  317. struct stat filestat;
  318. /* We need a writable copy of this string */
  319. path = strdup(path_list);
  320. if (!path) {
  321. fprintf(stderr, "Out of memory!\n");
  322. exit(EXIT_FAILURE);
  323. }
  324. /* Eliminate all double //s */
  325. path_n = path;
  326. while ((path_n = strstr(path_n, "//"))) {
  327. i = strlen(path_n);
  328. memmove(path_n, path_n + 1, i - 1);
  329. *(path_n + i - 1) = '\0';
  330. }
  331. /* Replace colons with zeros in path_list and count them */
  332. for (i = strlen(path); i > 0; i--) {
  333. if (path[i] == ':') {
  334. path[i] = 0;
  335. count++;
  336. }
  337. }
  338. path_n = path;
  339. for (i = 0; i < count; i++) {
  340. strcpy(result, path_n);
  341. strcat(result, "/");
  342. strcat(result, name);
  343. if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
  344. free(path);
  345. return;
  346. }
  347. path_n += (strlen(path_n) + 1);
  348. }
  349. free(path);
  350. *result = '\0';
  351. }
  352. void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_suid,
  353. struct library *lib)
  354. {
  355. char *buf;
  356. char *path;
  357. struct stat filestat;
  358. /* If this is a fully resolved name, our job is easy */
  359. if (stat(lib->name, &filestat) == 0) {
  360. lib->path = strdup(lib->name);
  361. return;
  362. }
  363. /* We need some elbow room here. Make some room... */
  364. buf = malloc(1024);
  365. if (!buf) {
  366. fprintf(stderr, "Out of memory!\n");
  367. exit(EXIT_FAILURE);
  368. }
  369. /* This function must match the behavior of _dl_load_shared_library
  370. * in readelflib1.c or things won't work out as expected... */
  371. /* The ABI specifies that RPATH is searched first, so do that now. */
  372. path = elf_find_rpath(ehdr, dynamic);
  373. if (path) {
  374. search_for_named_library(lib->name, buf, path);
  375. if (*buf != '\0') {
  376. lib->path = buf;
  377. return;
  378. }
  379. }
  380. /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
  381. * Since this app doesn't actually run an executable I will skip
  382. * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
  383. if (is_suid == 1)
  384. path = NULL;
  385. else
  386. path = getenv("LD_LIBRARY_PATH");
  387. if (path) {
  388. search_for_named_library(lib->name, buf, path);
  389. if (*buf != '\0') {
  390. lib->path = buf;
  391. return;
  392. }
  393. }
  394. #ifdef __LDSO_CACHE_SUPPORT__
  395. if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
  396. int i;
  397. header_t *header = (header_t *) cache_addr;
  398. libentry_t *libent = (libentry_t *) & header[1];
  399. char *strs = (char *)&libent[header->nlibs];
  400. for (i = 0; i < header->nlibs; i++) {
  401. if ((libent[i].flags == LIB_ELF ||
  402. libent[i].flags == LIB_ELF_LIBC0 ||
  403. libent[i].flags == LIB_ELF_LIBC5) &&
  404. strcmp(lib->name, strs + libent[i].sooffset) == 0)
  405. {
  406. lib->path = strdup(strs + libent[i].liboffset);
  407. return;
  408. }
  409. }
  410. }
  411. #endif
  412. /* Next look for libraries wherever the shared library
  413. * loader was installed -- this is usually where we
  414. * should find things... */
  415. if (interp_dir) {
  416. search_for_named_library(lib->name, buf, interp_dir);
  417. if (*buf != '\0') {
  418. lib->path = buf;
  419. return;
  420. }
  421. }
  422. /* Lastly, search the standard list of paths for the library.
  423. This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
  424. path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
  425. #ifndef __LDSO_CACHE_SUPPORT__
  426. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  427. #endif
  428. ;
  429. search_for_named_library(lib->name, buf, path);
  430. if (*buf != '\0') {
  431. lib->path = buf;
  432. } else {
  433. free(buf);
  434. lib->path = not_found;
  435. }
  436. }
  437. static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
  438. {
  439. char *tmp, *tmp1, *tmp2;
  440. struct library *cur, *newlib = lib_list;
  441. if (!s || !strlen(s))
  442. return 1;
  443. tmp = s;
  444. while (*tmp) {
  445. if (*tmp == '/')
  446. s = tmp + 1;
  447. tmp++;
  448. }
  449. /* We add ldso elsewhere */
  450. if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
  451. int len = strlen(interp_dir);
  452. if (strcmp(s, interp_name + 1 + len) == 0)
  453. return 1;
  454. }
  455. for (cur = lib_list; cur; cur = cur->next) {
  456. /* Check if this library is already in the list */
  457. tmp1 = tmp2 = cur->name;
  458. while (*tmp1) {
  459. if (*tmp1 == '/')
  460. tmp2 = tmp1 + 1;
  461. tmp1++;
  462. }
  463. if (strcmp(tmp2, s) == 0) {
  464. /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
  465. return 0;
  466. }
  467. }
  468. /* Ok, this lib needs to be added to the list */
  469. newlib = malloc(sizeof(struct library));
  470. if (!newlib)
  471. return 1;
  472. newlib->name = malloc(strlen(s) + 1);
  473. strcpy(newlib->name, s);
  474. newlib->resolved = 0;
  475. newlib->path = NULL;
  476. newlib->next = NULL;
  477. /* Now try and locate where this library might be living... */
  478. locate_library_file(ehdr, dynamic, is_setuid, newlib);
  479. /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
  480. if (!lib_list) {
  481. lib_list = newlib;
  482. } else {
  483. for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
  484. cur->next = newlib;
  485. }
  486. return 0;
  487. }
  488. static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
  489. {
  490. ElfW(Dyn) *dyns;
  491. for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
  492. if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
  493. char *strtab;
  494. strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  495. add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
  496. }
  497. }
  498. }
  499. static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
  500. {
  501. ElfW(Phdr) *phdr;
  502. if (interpreter_already_found == 1)
  503. return NULL;
  504. phdr = elf_find_phdr_type(PT_INTERP, ehdr);
  505. if (phdr) {
  506. struct library *cur, *newlib = NULL;
  507. char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
  508. char *tmp, *tmp1;
  509. interp_name = strdup(s);
  510. interp_dir = strdup(s);
  511. tmp = strrchr(interp_dir, '/');
  512. if (*tmp)
  513. *tmp = '\0';
  514. else {
  515. free(interp_dir);
  516. interp_dir = interp_name;
  517. }
  518. tmp1 = tmp = s;
  519. while (*tmp) {
  520. if (*tmp == '/')
  521. tmp1 = tmp + 1;
  522. tmp++;
  523. }
  524. for (cur = lib_list; cur; cur = cur->next) {
  525. /* Check if this library is already in the list */
  526. if (strcmp(cur->name, tmp1) == 0) {
  527. /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
  528. newlib = cur;
  529. free(newlib->name);
  530. if (newlib->path != not_found) {
  531. free(newlib->path);
  532. }
  533. newlib->name = NULL;
  534. newlib->path = NULL;
  535. return NULL;
  536. }
  537. }
  538. if (newlib == NULL)
  539. newlib = malloc(sizeof(struct library));
  540. if (!newlib)
  541. return NULL;
  542. newlib->name = malloc(strlen(s) + 1);
  543. strcpy(newlib->name, s);
  544. newlib->path = strdup(newlib->name);
  545. newlib->resolved = 1;
  546. newlib->next = NULL;
  547. #if 0
  548. /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
  549. if (!lib_list) {
  550. lib_list = newlib;
  551. } else {
  552. for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
  553. cur->next = newlib;
  554. }
  555. #endif
  556. interpreter_already_found = 1;
  557. return newlib;
  558. }
  559. return NULL;
  560. }
  561. /* map the .so, and locate interesting pieces */
  562. /*
  563. #warning "There may be two warnings here about vfork() clobbering, ignore them"
  564. */
  565. int find_dependancies(char *filename)
  566. {
  567. int is_suid = 0;
  568. FILE *thefile;
  569. struct library *interp;
  570. struct stat statbuf;
  571. ElfW(Ehdr) *ehdr = NULL;
  572. ElfW(Shdr) *dynsec = NULL;
  573. ElfW(Dyn) *dynamic = NULL;
  574. if (filename == not_found)
  575. return 0;
  576. if (!filename) {
  577. fprintf(stderr, "No filename specified.\n");
  578. return -1;
  579. }
  580. if (!(thefile = fopen(filename, "r"))) {
  581. perror(filename);
  582. return -1;
  583. }
  584. if (fstat(fileno(thefile), &statbuf) < 0) {
  585. perror(filename);
  586. fclose(thefile);
  587. return -1;
  588. }
  589. if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
  590. goto foo;
  591. if (!S_ISREG(statbuf.st_mode))
  592. goto foo;
  593. /* mmap the file to make reading stuff from it effortless */
  594. ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
  595. if (ehdr == MAP_FAILED) {
  596. fclose(thefile);
  597. fprintf(stderr, "Out of memory!\n");
  598. return -1;
  599. }
  600. foo:
  601. fclose(thefile);
  602. /* Check if this looks like a legit ELF file */
  603. if (check_elf_header(ehdr)) {
  604. fprintf(stderr, "%s: not an ELF file.\n", filename);
  605. return -1;
  606. }
  607. /* Check if this is the right kind of ELF file */
  608. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
  609. fprintf(stderr, "%s: not a dynamic executable\n", filename);
  610. return -1;
  611. }
  612. if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
  613. if (statbuf.st_mode & S_ISUID)
  614. is_suid = 1;
  615. if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  616. is_suid = 1;
  617. /* FIXME */
  618. if (is_suid)
  619. fprintf(stderr, "%s: is setuid\n", filename);
  620. }
  621. interpreter_already_found = 0;
  622. interp = find_elf_interpreter(ehdr);
  623. #ifdef __LDSO_LDD_SUPPORT__
  624. if (interp
  625. && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
  626. && ehdr->e_ident[EI_CLASS] == ELFCLASSM
  627. && ehdr->e_ident[EI_DATA] == ELFDATAM
  628. && ehdr->e_ident[EI_VERSION] == EV_CURRENT
  629. && MATCH_MACHINE(ehdr->e_machine))
  630. {
  631. struct stat statbuf;
  632. if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
  633. pid_t pid;
  634. int status;
  635. static const char *const environment[] = {
  636. "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
  637. "SHELL=/bin/sh",
  638. "LD_TRACE_LOADED_OBJECTS=1",
  639. NULL
  640. };
  641. if ((pid = vfork()) == 0) {
  642. /* Cool, it looks like we should be able to actually
  643. * run this puppy. Do so now... */
  644. execle(filename, filename, NULL, environment);
  645. _exit(0xdead);
  646. }
  647. /* Wait till it returns */
  648. waitpid(pid, &status, 0);
  649. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  650. return 1;
  651. }
  652. /* If the exec failed, we fall through to trying to find
  653. * all the needed libraries ourselves by rummaging about
  654. * in the ELF headers... */
  655. }
  656. }
  657. #endif
  658. dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
  659. if (dynsec) {
  660. dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
  661. find_needed_libraries(ehdr, dynamic, is_suid);
  662. }
  663. return 0;
  664. }
  665. int main(int argc, char **argv)
  666. {
  667. int multi = 0;
  668. int got_em_all = 1;
  669. char *filename = NULL;
  670. struct library *cur;
  671. if (argc < 2) {
  672. fprintf(stderr, "ldd: missing file arguments\n");
  673. fprintf(stderr, "Try `ldd --help' for more information.\n");
  674. exit(EXIT_FAILURE);
  675. }
  676. if (argc > 2)
  677. multi++;
  678. while (--argc > 0) {
  679. ++argv;
  680. if (strcmp(*argv, "--") == 0) {
  681. /* Ignore "--" */
  682. continue;
  683. }
  684. if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
  685. fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
  686. fprintf(stderr, "\t--help\t\tprint this help and exit\n");
  687. exit(EXIT_SUCCESS);
  688. }
  689. filename = *argv;
  690. if (!filename) {
  691. fprintf(stderr, "No filename specified.\n");
  692. exit(EXIT_FAILURE);
  693. }
  694. if (multi) {
  695. printf("%s:\n", *argv);
  696. }
  697. map_cache();
  698. if (find_dependancies(filename) != 0)
  699. continue;
  700. while (got_em_all) {
  701. got_em_all = 0;
  702. /* Keep walking the list till everybody is resolved */
  703. for (cur = lib_list; cur; cur = cur->next) {
  704. if (cur->resolved == 0 && cur->path) {
  705. got_em_all = 1;
  706. printf("checking sub-depends for '%s'\n", cur->path);
  707. find_dependancies(cur->path);
  708. cur->resolved = 1;
  709. }
  710. }
  711. }
  712. unmap_cache();
  713. /* Print the list */
  714. got_em_all = 0;
  715. for (cur = lib_list; cur; cur = cur->next) {
  716. got_em_all = 1;
  717. printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
  718. }
  719. if (interp_name && interpreter_already_found == 1)
  720. printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
  721. else
  722. printf("\tnot a dynamic executable\n");
  723. for (cur = lib_list; cur; cur = cur->next) {
  724. free(cur->name);
  725. cur->name = NULL;
  726. if (cur->path && cur->path != not_found) {
  727. free(cur->path);
  728. cur->path = NULL;
  729. }
  730. }
  731. lib_list = NULL;
  732. }
  733. return 0;
  734. }