ldd.c 11 KB

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