ldd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. char not_found[] = "not found";
  46. Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr)
  47. {
  48. int j;
  49. Elf32_Shdr *shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr);
  50. for (j = ehdr->e_shnum; --j>=0; ++shdr) {
  51. if (shdr->sh_type == key) {
  52. return shdr;
  53. }
  54. }
  55. return NULL;
  56. }
  57. Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr)
  58. {
  59. int j;
  60. Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr);
  61. for (j = ehdr->e_phnum; --j>=0; ++phdr) {
  62. if (type==phdr->p_type) {
  63. return phdr;
  64. }
  65. }
  66. return NULL;
  67. }
  68. /* Returns value if return_val==1, ptr otherwise */
  69. void * elf_find_dynamic(int const key, Elf32_Dyn *dynp,
  70. Elf32_Ehdr *ehdr, int return_val)
  71. {
  72. Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
  73. unsigned tx_reloc = pt_text->p_vaddr - pt_text->p_offset;
  74. for (; DT_NULL!=dynp->d_tag; ++dynp) {
  75. if (dynp->d_tag == key) {
  76. if (return_val == 1)
  77. return (void *)dynp->d_un.d_val;
  78. else
  79. return (void *)(dynp->d_un.d_val - tx_reloc + (char *)ehdr );
  80. }
  81. }
  82. return NULL;
  83. }
  84. int check_elf_header(Elf32_Ehdr const *const ehdr)
  85. {
  86. if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||
  87. ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
  88. ehdr->e_ident[EI_VERSION] != EV_CURRENT)
  89. {
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. /* This function's behavior must exactly match that
  95. * in uClibc/ldso/d-link/readelflib1.c */
  96. static void search_for_named_library(char *name, char *result, const char *path_list)
  97. {
  98. int i, count = 0;
  99. char *path, *path_n;
  100. struct stat filestat;
  101. /* We need a writable copy of this string */
  102. path = strdup(path_list);
  103. if (!path) {
  104. fprintf(stderr, "Out of memory!\n");
  105. exit(EXIT_FAILURE);
  106. }
  107. /* Eliminate all double //s */
  108. path_n=path;
  109. while((path_n=strstr(path_n, "//"))) {
  110. i = strlen(path_n);
  111. memmove(path_n, path_n+1, i-1);
  112. }
  113. /* Replace colons with zeros in path_list and count them */
  114. for(i=strlen(path); i > 0; i--) {
  115. if (path[i]==':') {
  116. path[i]=0;
  117. count++;
  118. }
  119. }
  120. path_n = path;
  121. for (i = 0; i < count; i++) {
  122. *result = '\0';
  123. strcat(result, path_n);
  124. strcat(result, "/");
  125. strcat(result, name);
  126. if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
  127. free(path);
  128. return;
  129. }
  130. path_n += (strlen(path_n) + 1);
  131. }
  132. free(path);
  133. *result = '\0';
  134. }
  135. void locate_library_file(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_suid, struct library *lib)
  136. {
  137. char *buf;
  138. char *path;
  139. struct stat filestat;
  140. /* If this is a fully resolved name, our job is easy */
  141. if (stat (lib->name, &filestat) == 0) {
  142. lib->path = lib->name;
  143. return;
  144. }
  145. /* We need some elbow room here. Make some room...*/
  146. buf = malloc(1024);
  147. if (!buf) {
  148. fprintf(stderr, "Out of memory!\n");
  149. exit(EXIT_FAILURE);
  150. }
  151. /* This function must match the behavior of _dl_load_shared_library
  152. * in readelflib1.c or things won't work out as expected... */
  153. /* The ABI specifies that RPATH is searched first, so do that now. */
  154. path = (char *)elf_find_dynamic(DT_RPATH, dynamic, ehdr, 0);
  155. if (path) {
  156. search_for_named_library(lib->name, buf, path);
  157. if (*buf != '\0') {
  158. lib->path = buf;
  159. return;
  160. } else {
  161. free(buf);
  162. }
  163. }
  164. /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
  165. * Since this app doesn't actually run an executable I will skip
  166. * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
  167. if (is_suid==1)
  168. path = NULL;
  169. else
  170. path = getenv("LD_LIBRARY_PATH");
  171. if (path) {
  172. search_for_named_library(lib->name, buf, path);
  173. if (*buf != '\0') {
  174. lib->path = buf;
  175. return;
  176. } else {
  177. free(buf);
  178. }
  179. }
  180. #ifdef USE_CACHE
  181. /* FIXME -- add code to check the Cache here */
  182. #endif
  183. /* Lastly, search the standard list of paths for the library.
  184. This list must exactly match the list in uClibc/ldso/d-link/readelflib1.c */
  185. path = UCLIBC_TARGET_PREFIX "/usr/lib:"
  186. UCLIBC_TARGET_PREFIX "/lib:"
  187. UCLIBC_DEVEL_PREFIX "/lib:"
  188. UCLIBC_BUILD_DIR "/lib:"
  189. "/usr/lib:"
  190. "/lib";
  191. search_for_named_library(lib->name, buf, path);
  192. if (*buf != '\0') {
  193. lib->path = buf;
  194. } else {
  195. free(buf);
  196. printf("bad stuff\n");
  197. lib->path = not_found;
  198. }
  199. }
  200. static int add_library(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid, const char *s)
  201. {
  202. struct library *cur, *prev, *newlib=lib_list;
  203. if (!s || !strlen(s))
  204. return 1;
  205. /* We add libc.so.0 elsewhere */
  206. if (strcmp(s, UCLIBC_LDSO)==0)
  207. return 1;
  208. for (cur = lib_list; cur; cur=cur->next) {
  209. if(strcmp(cur->name, s)==0) {
  210. /* Lib is already in the list */
  211. return 0;
  212. }
  213. }
  214. /* Ok, this lib needs to be added to the list */
  215. newlib = malloc(sizeof(struct library));
  216. if (!newlib)
  217. return 1;
  218. newlib->name = malloc(strlen(s));
  219. strcpy(newlib->name, s);
  220. newlib->resolved = 0;
  221. newlib->path = NULL;
  222. newlib->next = NULL;
  223. /* Now try and locate where this library might be living... */
  224. locate_library_file(ehdr, dynamic, strtab, is_setuid, newlib);
  225. //printf("adding '%s' to '%s'\n", newlib->name, newlib->path);
  226. if (!lib_list) {
  227. lib_list = newlib;
  228. } else {
  229. for (cur = prev = lib_list; cur->next; prev=cur, cur=cur->next); /* nothing */
  230. cur = newlib;
  231. prev->next = cur;
  232. }
  233. return 0;
  234. }
  235. static void find_needed_libraries(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid)
  236. {
  237. Elf32_Dyn *dyns;
  238. for (dyns=dynamic; dyns->d_tag!=DT_NULL; ++dyns) {
  239. if (dyns->d_tag == DT_NEEDED) {
  240. add_library(ehdr, dynamic, strtab, is_setuid, (char*)strtab + dyns->d_un.d_val);
  241. }
  242. }
  243. }
  244. static void find_elf_interpreter(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid)
  245. {
  246. Elf32_Phdr *phdr;
  247. phdr = elf_find_phdr_type(PT_INTERP, ehdr);
  248. if (phdr) {
  249. struct library *cur, *prev, *newlib=lib_list;
  250. char *s = (char*)ehdr + phdr->p_offset;
  251. for (cur = lib_list; cur; cur=cur->next) {
  252. if(strcmp(cur->name, s)==0) {
  253. /* Lib is already in the list */
  254. return;
  255. }
  256. }
  257. newlib = malloc(sizeof(struct library));
  258. if (!newlib)
  259. return;
  260. newlib->name = malloc(strlen(s));
  261. strcpy(newlib->name, s);
  262. newlib->path = newlib->name;
  263. newlib->resolved = 1;
  264. newlib->next = NULL;
  265. //printf("adding '%s' to '%s'\n", newlib->name, newlib->path);
  266. if (!lib_list) {
  267. lib_list = newlib;
  268. } else {
  269. for (cur = prev = lib_list; cur->next; prev=cur, cur=cur->next); /* nothing */
  270. cur = newlib;
  271. prev->next = cur;
  272. }
  273. }
  274. }
  275. /* map the .so, and locate interesting pieces */
  276. int find_dependancies(char* filename)
  277. {
  278. int is_suid = 0;
  279. FILE *thefile;
  280. struct stat statbuf;
  281. char *dynstr=NULL;
  282. Elf32_Ehdr *ehdr = NULL;
  283. Elf32_Shdr *dynsec = NULL;
  284. Elf32_Dyn *dynamic = NULL;
  285. if (filename == not_found)
  286. return 0;
  287. if (!filename) {
  288. fprintf(stderr, "No filename specified.\n");
  289. exit(EXIT_FAILURE);
  290. }
  291. if (!(thefile = fopen(filename, "r"))) {
  292. perror(filename);
  293. exit(EXIT_FAILURE);
  294. }
  295. if (fstat(fileno(thefile), &statbuf) < 0) {
  296. perror(filename);
  297. exit(EXIT_FAILURE);
  298. }
  299. if (statbuf.st_size < sizeof(Elf32_Ehdr))
  300. goto foo;
  301. /* mmap the file to make reading stuff from it effortless */
  302. ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size,
  303. PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
  304. foo:
  305. /* Check if this looks like a legit ELF file */
  306. if (check_elf_header(ehdr)) {
  307. fprintf(stderr, "%s: not an ELF file.\n", filename);
  308. exit(EXIT_FAILURE);
  309. }
  310. /* Check if this is the right kind of ELF file */
  311. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
  312. fprintf(stderr, "%s: not a dynamic executable\n", filename);
  313. exit(EXIT_FAILURE);
  314. }
  315. if (ehdr->e_type == ET_EXEC) {
  316. if (statbuf.st_mode & S_ISUID)
  317. is_suid = 1;
  318. if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  319. is_suid = 1;
  320. /* FIXME */
  321. if (is_suid)
  322. fprintf(stderr, "%s: is setuid\n", filename);
  323. }
  324. dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
  325. if (dynsec) {
  326. dynamic = (Elf32_Dyn*)(dynsec->sh_offset + (int)ehdr);
  327. dynstr = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  328. find_needed_libraries(ehdr, dynamic, dynstr, is_suid);
  329. }
  330. find_elf_interpreter(ehdr, dynamic, dynstr, is_suid);
  331. return 0;
  332. }
  333. int main( int argc, char** argv)
  334. {
  335. int got_em_all=1;
  336. char *filename = argv[1];
  337. struct library *cur;
  338. if (!filename) {
  339. fprintf(stderr, "No filename specified.\n");
  340. exit(EXIT_FAILURE);
  341. }
  342. find_dependancies(filename);
  343. while(got_em_all) {
  344. got_em_all=0;
  345. /* Keep walking the list till everybody is resolved */
  346. for (cur = lib_list; cur; cur=cur->next) {
  347. if (cur->resolved == 0 && cur->path) {
  348. got_em_all=1;
  349. //printf("checking sub-depends for '%s\n", cur->path);
  350. find_dependancies(cur->path);
  351. cur->resolved = 1;
  352. }
  353. }
  354. }
  355. /* Print the list */
  356. got_em_all=0;
  357. for (cur = lib_list; cur; cur=cur->next) {
  358. got_em_all=1;
  359. printf("\t%s => %s\n", cur->name, cur->path);
  360. }
  361. if (got_em_all==0)
  362. printf("\tnot a dynamic executable\n");
  363. return 0;
  364. }