ldd.c 20 KB

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