ldd.c 21 KB

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