ldd.c 19 KB

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