ldd.c 21 KB

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