ldd.c 21 KB

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