ldd.c 20 KB

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