ldd.c 21 KB

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