ldd.c 20 KB

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