ldd.c 20 KB

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