ldd.c 12 KB

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