ldd.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * A small little ldd implementation for uClibc
  4. *
  5. * Copyright (C) 2001 by Lineo, inc.
  6. * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
  7. *
  8. * Several functions in this file (specifically, elf_find_section_type(),
  9. * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
  10. * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
  11. * <jreiser@BitWagon.com>, and which is copyright 2000 BitWagon Software LLC
  12. * (GPL2).
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <elf.h>
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <sys/mman.h>
  36. #include <sys/stat.h>
  37. #include <sys/types.h>
  38. struct library {
  39. char *name;
  40. int resolved;
  41. char *path;
  42. struct library *next;
  43. };
  44. struct library *lib_list = NULL;
  45. Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr)
  46. {
  47. int j;
  48. Elf32_Shdr *shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr);
  49. for (j = ehdr->e_shnum; --j>=0; ++shdr) {
  50. if (shdr->sh_type == key) {
  51. return shdr;
  52. }
  53. }
  54. return NULL;
  55. }
  56. Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr)
  57. {
  58. int j;
  59. Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr);
  60. for (j = ehdr->e_phnum; --j>=0; ++phdr) {
  61. if (type==phdr->p_type) {
  62. return phdr;
  63. }
  64. }
  65. return NULL;
  66. }
  67. /* Returns value if return_val==1, ptr otherwise */
  68. void * elf_find_dynamic(int const key, Elf32_Dyn *dynp,
  69. Elf32_Ehdr *ehdr, int return_val)
  70. {
  71. Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
  72. unsigned tx_reloc = pt_text->p_vaddr - pt_text->p_offset;
  73. for (; DT_NULL!=dynp->d_tag; ++dynp) {
  74. if (dynp->d_tag == key) {
  75. if (return_val == 1)
  76. return (void *)dynp->d_un.d_val;
  77. else
  78. return (void *)(dynp->d_un.d_val - tx_reloc + (char *)ehdr );
  79. }
  80. }
  81. return NULL;
  82. }
  83. int check_elf_header(Elf32_Ehdr const *const ehdr)
  84. {
  85. if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||
  86. ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
  87. ehdr->e_ident[EI_VERSION] != EV_CURRENT)
  88. {
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. char * last_char_is(const char *s, int c)
  94. {
  95. char *sret;
  96. if (!s)
  97. return NULL;
  98. sret = (char *)s+strlen(s)-1;
  99. if (sret>=s && *sret == c) {
  100. return sret;
  101. } else {
  102. return NULL;
  103. }
  104. }
  105. extern char *concat_path_file(const char *path, const char *filename)
  106. {
  107. char *outbuf;
  108. char *lc;
  109. if (!path)
  110. path="";
  111. lc = last_char_is(path, '/');
  112. if (filename[0] == '/')
  113. filename++;
  114. outbuf = malloc(strlen(path)+strlen(filename)+1+(lc==NULL));
  115. if (!outbuf) {
  116. fprintf(stderr, "out of memory\n");
  117. exit(EXIT_FAILURE);
  118. }
  119. sprintf(outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename);
  120. return outbuf;
  121. }
  122. char *do_which(char *name, char *path_list)
  123. {
  124. char *path_n;
  125. char *path_tmp;
  126. struct stat filestat;
  127. int i, count=1;
  128. if (!path_list) {
  129. fprintf(stderr, "yipe!\n");
  130. exit(EXIT_FAILURE);
  131. }
  132. /* We need a writable copy of this string */
  133. path_tmp = strdup(path_list);
  134. if (!path_tmp) {
  135. fprintf(stderr, "yipe!\n");
  136. exit(EXIT_FAILURE);
  137. }
  138. /* Replace colons with zeros in path_parsed and count them */
  139. for(i=strlen(path_tmp); i > 0; i--)
  140. if (path_tmp[i]==':') {
  141. path_tmp[i]=0;
  142. count++;
  143. }
  144. path_n = path_tmp;
  145. for (i = 0; i < count; i++) {
  146. char *buf;
  147. buf = concat_path_file(path_n, name);
  148. if (stat (buf, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
  149. return(buf);
  150. }
  151. free(buf);
  152. path_n += (strlen(path_n) + 1);
  153. }
  154. return NULL;
  155. }
  156. void locate_library_file(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_suid, struct library *lib)
  157. {
  158. char *buf;
  159. char *path;
  160. struct stat filestat;
  161. lib->path = "not found";
  162. /* If this is a fully resolved name, our job is easy */
  163. if (stat (lib->name, &filestat) == 0) {
  164. lib->path = lib->name;
  165. return;
  166. }
  167. /* This function must match the behavior of _dl_load_shared_library
  168. * in readelflib1.c or things won't work out as expected... */
  169. /* The ABI specifies that RPATH is searched before LD_*_PATH or the
  170. * default path (such as /usr/lib). So first, lets check the rpath
  171. * directories */
  172. path = (char *)elf_find_dynamic(DT_RPATH, dynamic, ehdr, 0);
  173. if (path) {
  174. buf = do_which(lib->name, path);
  175. if (buf) {
  176. lib->path = buf;
  177. return;
  178. }
  179. }
  180. /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
  181. * Since this app doesn't actually run an executable I will skip
  182. * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
  183. if (is_suid==1)
  184. path = NULL;
  185. else
  186. path = getenv("LD_LIBRARY_PATH");
  187. if (path) {
  188. buf = do_which(lib->name, path);
  189. if (buf) {
  190. lib->path = buf;
  191. return;
  192. }
  193. }
  194. /* Fixme -- add code to check the Cache here */
  195. buf = do_which(lib->name, UCLIBC_ROOT_DIR "/usr/lib/:" UCLIBC_ROOT_DIR
  196. "/lib/:" UCLIBC_BUILD_DIR "/lib/:/usr/lib:/lib");
  197. if (buf) {
  198. lib->path = buf;
  199. }
  200. }
  201. static int add_library(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid, const char *s)
  202. {
  203. struct library *cur, *prev, *newlib=lib_list;
  204. if (!s || !strlen(s))
  205. return 1;
  206. for (cur = lib_list; cur; cur=cur->next) {
  207. if(strcmp(cur->name, s)==0) {
  208. /* Lib is already in the list */
  209. return 0;
  210. }
  211. }
  212. /* Ok, this lib needs to be added to the list */
  213. newlib = malloc(sizeof(struct library));
  214. if (!newlib)
  215. return 1;
  216. newlib->name = malloc(strlen(s));
  217. strcpy(newlib->name, s);
  218. newlib->resolved = 0;
  219. newlib->next = NULL;
  220. /* Now try and locate where this library might be living... */
  221. locate_library_file(ehdr, dynamic, strtab, is_setuid, newlib);
  222. //printf("adding '%s' to '%s'\n", newlib->name, newlib->path);
  223. if (!lib_list) {
  224. lib_list = newlib;
  225. } else {
  226. for (cur = prev = lib_list; cur->next; prev=cur, cur=cur->next); /* nothing */
  227. cur = newlib;
  228. prev->next = cur;
  229. }
  230. return 0;
  231. }
  232. static void find_needed_libraries(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid)
  233. {
  234. Elf32_Dyn *dyns;
  235. for (dyns=dynamic; dyns->d_tag!=DT_NULL; ++dyns) {
  236. if (dyns->d_tag == DT_NEEDED) {
  237. add_library(ehdr, dynamic, strtab, is_setuid, (char*)strtab + dyns->d_un.d_val);
  238. }
  239. }
  240. }
  241. static void find_elf_interpreter(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid)
  242. {
  243. Elf32_Phdr *phdr;
  244. phdr = elf_find_phdr_type(PT_INTERP, ehdr);
  245. if (phdr) {
  246. add_library(ehdr, dynamic, strtab, is_setuid, (char*)ehdr + phdr->p_offset);
  247. }
  248. }
  249. /* map the .so, and locate interesting pieces */
  250. int find_dependancies(char* filename)
  251. {
  252. int is_suid = 0;
  253. FILE *thefile;
  254. struct stat statbuf;
  255. char *dynstr=NULL;
  256. Elf32_Ehdr *ehdr = NULL;
  257. Elf32_Shdr *dynsec = NULL;
  258. Elf32_Dyn *dynamic = NULL;
  259. if (!filename) {
  260. fprintf(stderr, "No filename specified.\n");
  261. exit(EXIT_FAILURE);
  262. }
  263. if (!(thefile = fopen(filename, "r"))) {
  264. perror(filename);
  265. exit(EXIT_FAILURE);
  266. }
  267. if (fstat(fileno(thefile), &statbuf) < 0) {
  268. perror(filename);
  269. exit(EXIT_FAILURE);
  270. }
  271. if (statbuf.st_size < sizeof(Elf32_Ehdr))
  272. goto foo;
  273. /* mmap the file to make reading stuff from it effortless */
  274. ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size,
  275. PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
  276. foo:
  277. /* Check if this looks like a legit ELF file */
  278. if (check_elf_header(ehdr)) {
  279. fprintf(stderr, "%s: not an ELF file.\n", filename);
  280. exit(EXIT_FAILURE);
  281. }
  282. /* Check if this is the right kind of ELF file */
  283. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
  284. fprintf(stderr, "%s: not a dynamic executable\n", filename);
  285. exit(EXIT_FAILURE);
  286. }
  287. if (ehdr->e_type == ET_EXEC) {
  288. if (statbuf.st_mode & S_ISUID)
  289. is_suid = 1;
  290. if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  291. is_suid = 1;
  292. /* FIXME */
  293. if (is_suid)
  294. fprintf(stderr, "%s: is setuid\n", filename);
  295. }
  296. dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
  297. if (dynsec) {
  298. dynamic = (Elf32_Dyn*)(dynsec->sh_offset + (int)ehdr);
  299. dynstr = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  300. find_needed_libraries(ehdr, dynamic, dynstr, is_suid);
  301. }
  302. find_elf_interpreter(ehdr, dynamic, dynstr, is_suid);
  303. return 0;
  304. }
  305. int main( int argc, char** argv)
  306. {
  307. int got_em_all=1;
  308. char *filename = argv[1];
  309. struct library *cur;
  310. if (!filename) {
  311. fprintf(stderr, "No filename specified.\n");
  312. exit(EXIT_FAILURE);
  313. }
  314. find_dependancies(filename);
  315. while(got_em_all) {
  316. got_em_all=0;
  317. /* Keep walking the list till everybody is resolved */
  318. for (cur = lib_list; cur; cur=cur->next) {
  319. if (cur->resolved == 0 && cur->path) {
  320. got_em_all=1;
  321. //printf("checking sub-depends for '%s\n", cur->path);
  322. find_dependancies(cur->path);
  323. cur->resolved = 1;
  324. }
  325. }
  326. }
  327. /* Print the list */
  328. for (cur = lib_list; cur; cur=cur->next) {
  329. printf("\t%s => %s\n", cur->name, cur->path);
  330. }
  331. return 0;
  332. }