ldd.c 11 KB

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