ldd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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)
  254. || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
  255. fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
  256. cache_addr = (caddr_t) - 1; /* so we won't try again */
  257. return -1;
  258. }
  259. cache_size = st.st_size;
  260. cache_addr = (caddr_t) mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
  261. close(fd);
  262. if (cache_addr == MAP_FAILED) {
  263. fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
  264. return -1;
  265. }
  266. header = (header_t *) cache_addr;
  267. if (cache_size < sizeof(header_t) ||
  268. memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
  269. || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
  270. || cache_size <
  271. (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
  272. || cache_addr[cache_size - 1] != '\0')
  273. {
  274. fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
  275. goto fail;
  276. }
  277. strtabsize = cache_size - sizeof(header_t) -
  278. 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 ||
  282. libent[i].liboffset >= strtabsize)
  283. {
  284. fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
  285. goto fail;
  286. }
  287. }
  288. return 0;
  289. fail:
  290. munmap(cache_addr, cache_size);
  291. cache_addr = (caddr_t) - 1;
  292. return -1;
  293. }
  294. int unmap_cache(void)
  295. {
  296. if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
  297. return -1;
  298. #if 1
  299. munmap(cache_addr, cache_size);
  300. cache_addr = NULL;
  301. #endif
  302. return 0;
  303. }
  304. #else
  305. static inline void map_cache(void) { }
  306. static inline void unmap_cache(void) { }
  307. #endif
  308. /* This function's behavior must exactly match that
  309. * in uClibc/ldso/ldso/dl-elf.c */
  310. static void search_for_named_library(char *name, char *result, 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, 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. lib->path = strdup(strs + libent[i].liboffset);
  402. return;
  403. }
  404. }
  405. }
  406. #endif
  407. /* Next look for libraries wherever the shared library
  408. * loader was installed -- this is usually where we
  409. * should find things... */
  410. if (interp_dir) {
  411. search_for_named_library(lib->name, buf, interp_dir);
  412. if (*buf != '\0') {
  413. lib->path = buf;
  414. return;
  415. }
  416. }
  417. /* Lastly, search the standard list of paths for the library.
  418. This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
  419. path = UCLIBC_RUNTIME_PREFIX "lib:"
  420. 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. {
  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,
  486. ElfW(Dyn)* dynamic, int is_setuid)
  487. {
  488. ElfW(Dyn) *dyns;
  489. for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
  490. if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
  491. char *strtab;
  492. strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  493. add_library(ehdr, dynamic, is_setuid,
  494. (char*)strtab + byteswap_to_host(dyns->d_un.d_val));
  495. }
  496. }
  497. }
  498. static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr)
  499. {
  500. ElfW(Phdr) *phdr;
  501. if (interpreter_already_found == 1)
  502. return NULL;
  503. phdr = elf_find_phdr_type(PT_INTERP, ehdr);
  504. if (phdr) {
  505. struct library *cur, *newlib=NULL;
  506. char *s = (char*)ehdr + byteswap_to_host(phdr->p_offset);
  507. char *tmp, *tmp1;
  508. interp_name = strdup(s);
  509. interp_dir = strdup(s);
  510. tmp = strrchr(interp_dir, '/');
  511. if (*tmp)
  512. *tmp = '\0';
  513. else {
  514. free(interp_dir);
  515. interp_dir = interp_name;
  516. }
  517. tmp1 = tmp = s;
  518. while (*tmp) {
  519. if (*tmp == '/')
  520. tmp1 = tmp + 1;
  521. tmp++;
  522. }
  523. for (cur = lib_list; cur; cur=cur->next) {
  524. /* Check if this library is already in the list */
  525. if(strcmp(cur->name, tmp1)==0) {
  526. //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
  527. newlib = cur;
  528. free(newlib->name);
  529. if (newlib->path != not_found) {
  530. free(newlib->path);
  531. }
  532. newlib->name = NULL;
  533. newlib->path = NULL;
  534. return NULL;
  535. }
  536. }
  537. if (newlib == NULL)
  538. newlib = malloc(sizeof(struct library));
  539. if (!newlib)
  540. return NULL;
  541. newlib->name = malloc(strlen(s)+1);
  542. strcpy(newlib->name, s);
  543. newlib->path = strdup(newlib->name);
  544. newlib->resolved = 1;
  545. newlib->next = NULL;
  546. #if 0
  547. //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
  548. if (!lib_list) {
  549. lib_list = newlib;
  550. } else {
  551. for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
  552. cur->next = newlib;
  553. }
  554. #endif
  555. interpreter_already_found = 1;
  556. return newlib;
  557. }
  558. return NULL;
  559. }
  560. /* map the .so, and locate interesting pieces */
  561. /*
  562. #warning "There may be two warnings here about vfork() clobbering, ignore them"
  563. */
  564. int find_dependancies(char* filename)
  565. {
  566. int is_suid = 0;
  567. FILE *thefile;
  568. struct library *interp;
  569. struct stat statbuf;
  570. ElfW(Ehdr) *ehdr = NULL;
  571. ElfW(Shdr) *dynsec = NULL;
  572. ElfW(Dyn) *dynamic = NULL;
  573. if (filename == not_found)
  574. return 0;
  575. if (!filename) {
  576. fprintf(stderr, "No filename specified.\n");
  577. return -1;
  578. }
  579. if (!(thefile = fopen(filename, "r"))) {
  580. perror(filename);
  581. return -1;
  582. }
  583. if (fstat(fileno(thefile), &statbuf) < 0) {
  584. perror(filename);
  585. fclose(thefile);
  586. return -1;
  587. }
  588. if ((size_t)statbuf.st_size < sizeof(ElfW(Ehdr)))
  589. goto foo;
  590. if (!S_ISREG(statbuf.st_mode))
  591. goto foo;
  592. /* mmap the file to make reading stuff from it effortless */
  593. ehdr = (ElfW(Ehdr) *)mmap(0, statbuf.st_size,
  594. 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 && ehdr->e_ident[EI_DATA] == ELFDATAM && \
  627. ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine))
  628. {
  629. struct stat statbuf;
  630. if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
  631. pid_t pid;
  632. int status;
  633. static const char * const environment[] = {
  634. "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
  635. "SHELL=/bin/sh",
  636. "LD_TRACE_LOADED_OBJECTS=1",
  637. NULL
  638. };
  639. if ((pid = vfork()) == 0) {
  640. /* Cool, it looks like we should be able to actually
  641. * run this puppy. Do so now... */
  642. execle(filename, filename, NULL, environment);
  643. _exit(0xdead);
  644. }
  645. /* Wait till it returns */
  646. waitpid(pid, &status, 0);
  647. if (WIFEXITED(status) && WEXITSTATUS(status)==0) {
  648. return 1;
  649. }
  650. /* If the exec failed, we fall through to trying to find
  651. * all the needed libraries ourselves by rummaging about
  652. * in the ELF headers... */
  653. }
  654. }
  655. #endif
  656. dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
  657. if (dynsec) {
  658. dynamic = (ElfW(Dyn)*)(byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
  659. find_needed_libraries(ehdr, dynamic, is_suid);
  660. }
  661. return 0;
  662. }
  663. int main( int argc, char** argv)
  664. {
  665. int multi = 0;
  666. int got_em_all = 1;
  667. char *filename = NULL;
  668. struct library *cur;
  669. if (argc < 2) {
  670. fprintf(stderr, "ldd: missing file arguments\n");
  671. fprintf(stderr, "Try `ldd --help' for more information.\n");
  672. exit(EXIT_FAILURE);
  673. }
  674. if (argc > 2)
  675. multi++;
  676. while (--argc > 0) {
  677. ++argv;
  678. if(strcmp(*argv, "--")==0) {
  679. /* Ignore "--" */
  680. continue;
  681. }
  682. if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
  683. fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
  684. fprintf(stderr, "\t--help\t\tprint this help and exit\n");
  685. exit(EXIT_SUCCESS);
  686. }
  687. filename = *argv;
  688. if (!filename) {
  689. fprintf(stderr, "No filename specified.\n");
  690. exit(EXIT_FAILURE);
  691. }
  692. if (multi) {
  693. printf("%s:\n", *argv);
  694. }
  695. map_cache();
  696. if (find_dependancies(filename)!=0)
  697. continue;
  698. while (got_em_all) {
  699. got_em_all = 0;
  700. /* Keep walking the list till everybody is resolved */
  701. for (cur = lib_list; cur; cur=cur->next) {
  702. if (cur->resolved == 0 && cur->path) {
  703. got_em_all = 1;
  704. printf("checking sub-depends for '%s'\n", cur->path);
  705. find_dependancies(cur->path);
  706. cur->resolved = 1;
  707. }
  708. }
  709. }
  710. unmap_cache();
  711. /* Print the list */
  712. got_em_all = 0;
  713. for (cur = lib_list; cur; cur=cur->next) {
  714. got_em_all = 1;
  715. printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
  716. }
  717. if (interp_name && interpreter_already_found==1)
  718. printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
  719. else
  720. printf("\tnot a dynamic executable\n");
  721. for (cur = lib_list; cur; cur=cur->next) {
  722. free(cur->name);
  723. cur->name = NULL;
  724. if (cur->path && cur->path != not_found) {
  725. free(cur->path);
  726. cur->path = NULL;
  727. }
  728. }
  729. lib_list = NULL;
  730. }
  731. return 0;
  732. }