ldd.c 21 KB

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