ldd.c 21 KB

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