1 /* vi: set sw=4 ts=4: */
3 * A small little ldd implementation for uClibc
5 * Copyright (C) 2000-2004 Erik Andersen <andersee@debian.org>
7 * Several functions in this file (specifically, elf_find_section_type(),
8 * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
9 * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
10 * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include <sys/types.h>
39 #include <sys/types.h>
54 #define MATCH_MACHINE(x) (x == EM_ARM)
55 #define ELFCLASSM ELFCLASS32
59 #define MATCH_MACHINE(x) (x == EM_S390)
60 #define ELFCLASSM ELFCLASS32
65 #define MATCH_MACHINE(x) (x == EM_386)
67 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
69 #define ELFCLASSM ELFCLASS32
72 #if defined(__mc68000__)
73 #define MATCH_MACHINE(x) (x == EM_68K)
74 #define ELFCLASSM ELFCLASS32
78 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
79 #define ELFCLASSM ELFCLASS32
82 #if defined(__powerpc__)
83 #define MATCH_MACHINE(x) (x == EM_PPC)
84 #define ELFCLASSM ELFCLASS32
88 #define MATCH_MACHINE(x) (x == EM_SH)
89 #define ELFCLASSM ELFCLASS32
92 #if defined (__v850e__)
93 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
94 #define ELFCLASSM ELFCLASS32
97 #if defined (__sparc__)
98 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
99 #define ELFCLASSM ELFCLASS32
102 #if defined(__cris__)
103 #define MATCH_MACHINE(x) (x == EM_CRIS)
104 #define ELFCLASSM ELFCLASS32
107 #ifndef MATCH_MACHINE
108 #warning "You really should add a MATCH_MACHINE() macro for your architecture"
111 #if __BYTE_ORDER == __LITTLE_ENDIAN
112 #define ELFDATAM ELFDATA2LSB
113 #elif __BYTE_ORDER == __BIG_ENDIAN
114 #define ELFDATAM ELFDATA2MSB
121 struct library *next;
123 struct library *lib_list = NULL;
124 char not_found[] = "not found";
126 char *interp_dir = NULL;
128 static int interpreter_already_found=0;
130 inline uint32_t byteswap32_to_host(uint32_t value)
133 return(bswap_32(value));
139 Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr)
143 shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr);
144 for (j = ehdr->e_shnum; --j>=0; ++shdr) {
145 if (key==(int)byteswap32_to_host(shdr->sh_type)) {
152 Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr)
155 Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr);
156 for (j = ehdr->e_phnum; --j>=0; ++phdr) {
157 if (type==(int)byteswap32_to_host(phdr->p_type)) {
164 /* Returns value if return_val==1, ptr otherwise */
165 void * elf_find_dynamic(int const key, Elf32_Dyn *dynp,
166 Elf32_Ehdr *ehdr, int return_val)
168 Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
169 unsigned tx_reloc = byteswap32_to_host(pt_text->p_vaddr) - byteswap32_to_host(pt_text->p_offset);
170 for (; DT_NULL!=byteswap32_to_host(dynp->d_tag); ++dynp) {
171 if (key == (int)byteswap32_to_host(dynp->d_tag)) {
173 return (void *)(intptr_t)byteswap32_to_host(dynp->d_un.d_val);
175 return (void *)(byteswap32_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr );
181 static char * elf_find_rpath(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic)
185 for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
186 if (DT_RPATH == byteswap32_to_host(dyns->d_tag)) {
188 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
189 return ((char*)strtab + byteswap32_to_host(dyns->d_un.d_val));
195 int check_elf_header(Elf32_Ehdr *const ehdr)
197 if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||
198 ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
199 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
204 /* Check if the target endianness matches the host's endianness */
206 #if __BYTE_ORDER == __LITTLE_ENDIAN
207 if (ehdr->e_ident[5] == ELFDATA2MSB) {
208 /* Ick -- we will have to byte-swap everything */
211 #elif __BYTE_ORDER == __BIG_ENDIAN
212 if (ehdr->e_ident[5] == ELFDATA2LSB) {
213 /* Ick -- we will have to byte-swap everything */
217 #error Unknown host byte order!
220 /* Be vary lazy, and only byteswap the stuff we use */
222 ehdr->e_type=bswap_16(ehdr->e_type);
223 ehdr->e_phoff=bswap_32(ehdr->e_phoff);
224 ehdr->e_shoff=bswap_32(ehdr->e_shoff);
225 ehdr->e_phnum=bswap_16(ehdr->e_phnum);
226 ehdr->e_shnum=bswap_16(ehdr->e_shnum);
232 /* This function's behavior must exactly match that
233 * in uClibc/ldso/ldso/readelflib1.c */
234 static void search_for_named_library(char *name, char *result, const char *path_list)
238 struct stat filestat;
240 /* We need a writable copy of this string */
241 path = strdup(path_list);
243 fprintf(stderr, "Out of memory!\n");
246 /* Eliminate all double //s */
248 while((path_n=strstr(path_n, "//"))) {
250 memmove(path_n, path_n+1, i-1);
251 *(path_n + i - 1)='\0';
254 /* Replace colons with zeros in path_list and count them */
255 for(i=strlen(path); i > 0; i--) {
262 for (i = 0; i < count; i++) {
263 strcpy(result, path_n);
265 strcat(result, name);
266 if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
270 path_n += (strlen(path_n) + 1);
276 void locate_library_file(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, int is_suid, struct library *lib)
280 struct stat filestat;
282 /* If this is a fully resolved name, our job is easy */
283 if (stat (lib->name, &filestat) == 0) {
284 lib->path = lib->name;
288 /* We need some elbow room here. Make some room...*/
291 fprintf(stderr, "Out of memory!\n");
295 /* This function must match the behavior of _dl_load_shared_library
296 * in readelflib1.c or things won't work out as expected... */
298 /* The ABI specifies that RPATH is searched first, so do that now. */
299 path = elf_find_rpath(ehdr, dynamic);
301 search_for_named_library(lib->name, buf, path);
308 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
309 * Since this app doesn't actually run an executable I will skip
310 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
314 path = getenv("LD_LIBRARY_PATH");
316 search_for_named_library(lib->name, buf, path);
324 /* FIXME -- add code to check the Cache here */
328 /* Next look for libraries wherever the shared library
329 * loader was installed -- this is usually where we
330 * should find things... */
332 search_for_named_library(lib->name, buf, interp_dir);
339 /* Lastly, search the standard list of paths for the library.
340 This list must exactly match the list in uClibc/ldso/ldso/readelflib1.c */
341 path = UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib:"
342 UCLIBC_RUNTIME_PREFIX "usr/lib:"
343 UCLIBC_RUNTIME_PREFIX "lib:"
346 search_for_named_library(lib->name, buf, path);
351 lib->path = not_found;
355 static int add_library(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, int is_setuid, char *s)
357 char *tmp, *tmp1, *tmp2;
358 struct library *cur, *newlib=lib_list;
360 if (!s || !strlen(s))
370 /* We add libc.so.0 elsewhere */
371 if (interpreter_already_found && (tmp=strrchr(interp, '/')) != NULL)
373 int len = strlen(interp_dir);
374 if (strcmp(s, interp+1+len)==0)
378 for (cur = lib_list; cur; cur=cur->next) {
379 /* Check if this library is already in the list */
380 tmp1 = tmp2 = cur->name;
386 if(strcmp(tmp2, s)==0) {
387 //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
392 /* Ok, this lib needs to be added to the list */
393 newlib = malloc(sizeof(struct library));
396 newlib->name = malloc(strlen(s)+1);
397 strcpy(newlib->name, s);
398 newlib->resolved = 0;
402 /* Now try and locate where this library might be living... */
403 locate_library_file(ehdr, dynamic, is_setuid, newlib);
405 //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
409 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
415 static void find_needed_libraries(Elf32_Ehdr* ehdr,
416 Elf32_Dyn* dynamic, int is_setuid)
420 for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
421 if (DT_NEEDED == byteswap32_to_host(dyns->d_tag)) {
423 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
424 add_library(ehdr, dynamic, is_setuid,
425 (char*)strtab + byteswap32_to_host(dyns->d_un.d_val));
430 static struct library * find_elf_interpreter(Elf32_Ehdr* ehdr)
434 if (interpreter_already_found==1)
436 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
438 struct library *cur, *newlib=NULL;
439 char *s = (char*)ehdr + byteswap32_to_host(phdr->p_offset);
443 interp_dir = strdup(s);
444 tmp = strrchr(interp_dir, '/');
457 for (cur = lib_list; cur; cur=cur->next) {
458 /* Check if this library is already in the list */
459 if(strcmp(cur->name, tmp1)==0) {
460 //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
468 newlib = malloc(sizeof(struct library));
471 newlib->name = malloc(strlen(s)+1);
472 strcpy(newlib->name, s);
473 newlib->path = newlib->name;
474 newlib->resolved = 1;
478 //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
482 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
486 interpreter_already_found=1;
492 /* map the .so, and locate interesting pieces */
493 int find_dependancies(char* filename)
498 Elf32_Ehdr *ehdr = NULL;
499 Elf32_Shdr *dynsec = NULL;
500 Elf32_Dyn *dynamic = NULL;
501 struct library *interp;
503 if (filename == not_found)
507 fprintf(stderr, "No filename specified.\n");
510 if (!(thefile = fopen(filename, "r"))) {
514 if (fstat(fileno(thefile), &statbuf) < 0) {
519 if ((size_t)statbuf.st_size < sizeof(Elf32_Ehdr))
522 if (!S_ISREG(statbuf.st_mode))
525 /* mmap the file to make reading stuff from it effortless */
526 ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size,
527 PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
530 /* Check if this looks like a legit ELF file */
531 if (check_elf_header(ehdr)) {
532 fprintf(stderr, "%s: not an ELF file.\n", filename);
535 /* Check if this is the right kind of ELF file */
536 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
537 fprintf(stderr, "%s: not a dynamic executable\n", filename);
540 if (ehdr->e_type == ET_EXEC) {
541 if (statbuf.st_mode & S_ISUID)
543 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
547 fprintf(stderr, "%s: is setuid\n", filename);
550 interpreter_already_found=0;
551 interp = find_elf_interpreter(ehdr);
553 #ifdef __LDSO_LDD_SUPPORT
554 if (interp && ehdr->e_type == ET_EXEC && ehdr->e_ident[EI_CLASS] == ELFCLASSM &&
555 ehdr->e_ident[EI_DATA] == ELFDATAM
556 && ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine))
559 if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
562 static const char * const environment[] = {
563 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
565 "LD_TRACE_LOADED_OBJECTS=1",
569 if ((pid = fork()) == 0) {
570 /* Cool, it looks like we should be able to actually
571 * run this puppy. Do so now... */
572 execle(filename, filename, NULL, environment);
576 /* Wait till it returns */
577 waitpid(pid, &status, 0);
578 if (WIFEXITED(status) && WEXITSTATUS(status)==0) {
582 /* If the exec failed, we fall through to trying to find
583 * all the needed libraries ourselves by rummaging about
584 * in the ELF headers... */
589 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
591 dynamic = (Elf32_Dyn*)(byteswap32_to_host(dynsec->sh_offset) + (intptr_t)ehdr);
592 find_needed_libraries(ehdr, dynamic, is_suid);
598 int main( int argc, char** argv)
602 char *filename = NULL;
606 fprintf(stderr, "ldd: missing file arguments\n");
607 fprintf(stderr, "Try `ldd --help' for more information.\n");
617 if(strcmp(*argv, "--")==0) {
622 if(strcmp(*argv, "--help")==0) {
623 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
624 fprintf(stderr, "\t--help\t\tprint this help and exit\n");
630 fprintf(stderr, "No filename specified.\n");
635 printf("%s:\n", *argv);
638 if (find_dependancies(filename)!=0)
643 /* Keep walking the list till everybody is resolved */
644 for (cur = lib_list; cur; cur=cur->next) {
645 if (cur->resolved == 0 && cur->path) {
647 //printf("checking sub-depends for '%s\n", cur->path);
648 find_dependancies(cur->path);
657 for (cur = lib_list; cur; cur=cur->next) {
659 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
661 if (interp_dir && got_em_all==1)
662 printf("\t%s => %s (0x00000000)\n", interp, interp);
664 printf("\tnot a dynamic executable\n");
666 for (cur = lib_list; cur; cur=cur->next) {
667 if (cur->path && cur->path != not_found) {