ldd.c 21 KB

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