ldd.c 20 KB

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