ldd.c 22 KB

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