ldd.c 20 KB

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