ldd.c 21 KB

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