ldd.c 21 KB

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