ldd.c 21 KB

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