elf.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define _GNU_SOURCE
  14. #include <string.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <glob.h>
  18. #include <elf.h>
  19. #include <linux/limits.h>
  20. #include <libubox/utils.h>
  21. #include "elf.h"
  22. #include "fs.h"
  23. #include "log.h"
  24. struct avl_tree libraries;
  25. static LIST_HEAD(library_paths);
  26. static void alloc_library_path(const char *path)
  27. {
  28. struct stat s;
  29. if (stat(path, &s))
  30. return;
  31. struct library_path *p;
  32. char *_path;
  33. p = calloc_a(sizeof(*p),
  34. &_path, strlen(path) + 1);
  35. if (!p)
  36. return;
  37. p->path = strcpy(_path, path);
  38. list_add_tail(&p->list, &library_paths);
  39. DEBUG("adding ld.so path %s\n", path);
  40. }
  41. /*
  42. * path = full path
  43. * name = soname/avl key
  44. */
  45. void alloc_library(const char *path, const char *name)
  46. {
  47. struct library *l;
  48. char *_name, *_path;
  49. l = calloc_a(sizeof(*l),
  50. &_path, strlen(path) + 1,
  51. &_name, strlen(name) + 1);
  52. if (!l)
  53. return;
  54. l->avl.key = l->name = strcpy(_name, name);
  55. l->path = strcpy(_path, path);
  56. avl_insert(&libraries, &l->avl);
  57. DEBUG("adding library %s (%s)\n", path, name);
  58. }
  59. int lib_open(char **fullpath, const char *file)
  60. {
  61. struct library_path *p;
  62. char path[PATH_MAX];
  63. int fd = -1;
  64. *fullpath = NULL;
  65. list_for_each_entry(p, &library_paths, list) {
  66. snprintf(path, sizeof(path), "%s/%s", p->path, file);
  67. fd = open(path, O_RDONLY|O_CLOEXEC);
  68. if (fd >= 0) {
  69. *fullpath = strdup(path);
  70. break;
  71. }
  72. }
  73. return fd;
  74. }
  75. const char* find_lib(const char *file)
  76. {
  77. struct library *l;
  78. l = avl_find_element(&libraries, file, l, avl);
  79. if (!l)
  80. return NULL;
  81. return l->path;
  82. }
  83. static int elf64_find_section(const char *map, unsigned int type, unsigned long *offset, unsigned long *size, unsigned long *vaddr)
  84. {
  85. Elf64_Ehdr *e;
  86. Elf64_Phdr *ph;
  87. int i;
  88. e = (Elf64_Ehdr *) map;
  89. ph = (Elf64_Phdr *) (map + e->e_phoff);
  90. for (i = 0; i < e->e_phnum; i++) {
  91. if (ph[i].p_type == type) {
  92. *offset = ph[i].p_offset;
  93. if (size)
  94. *size = ph[i].p_filesz;
  95. if (vaddr)
  96. *vaddr = ph[i].p_vaddr;
  97. return 0;
  98. }
  99. }
  100. return -1;
  101. }
  102. static int elf32_find_section(const char *map, unsigned int type, unsigned long *offset, unsigned long *size, unsigned long *vaddr)
  103. {
  104. Elf32_Ehdr *e;
  105. Elf32_Phdr *ph;
  106. int i;
  107. e = (Elf32_Ehdr *) map;
  108. ph = (Elf32_Phdr *) (map + e->e_phoff);
  109. for (i = 0; i < e->e_phnum; i++) {
  110. if (ph[i].p_type == type) {
  111. *offset = ph[i].p_offset;
  112. if (size)
  113. *size = ph[i].p_filesz;
  114. if (vaddr)
  115. *vaddr = ph[i].p_vaddr;
  116. return 0;
  117. }
  118. }
  119. return -1;
  120. }
  121. static int elf_find_section(const char *map, unsigned int type, unsigned long *offset, unsigned long *size, unsigned long *vaddr)
  122. {
  123. int clazz = map[EI_CLASS];
  124. if (clazz == ELFCLASS32)
  125. return elf32_find_section(map, type, offset, size, vaddr);
  126. else if (clazz == ELFCLASS64)
  127. return elf64_find_section(map, type, offset, size, vaddr);
  128. ERROR("unknown elf format %d\n", clazz);
  129. return -1;
  130. }
  131. static int elf32_scan_dynamic(const char *map, unsigned long dyn_offset, unsigned long dyn_size, long load_offset)
  132. {
  133. Elf32_Dyn *dynamic = (Elf32_Dyn *) (map + dyn_offset);
  134. const char *strtab = NULL;
  135. while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
  136. Elf32_Dyn *curr = dynamic;
  137. dynamic++;
  138. if (curr->d_tag != DT_STRTAB)
  139. continue;
  140. strtab = map + (curr->d_un.d_ptr - load_offset);
  141. break;
  142. }
  143. if (!strtab)
  144. return -1;
  145. dynamic = (Elf32_Dyn *) (map + dyn_offset);
  146. while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
  147. Elf32_Dyn *curr = dynamic;
  148. dynamic++;
  149. if (curr->d_tag != DT_NEEDED)
  150. continue;
  151. if (add_path_and_deps(&strtab[curr->d_un.d_val], 1, -1, 1))
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. static int elf64_scan_dynamic(const char *map, unsigned long dyn_offset, unsigned long dyn_size, long load_offset)
  157. {
  158. Elf64_Dyn *dynamic = (Elf64_Dyn *) (map + dyn_offset);
  159. const char *strtab = NULL;
  160. while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
  161. Elf64_Dyn *curr = dynamic;
  162. dynamic++;
  163. if (curr->d_tag != DT_STRTAB)
  164. continue;
  165. strtab = map + (curr->d_un.d_ptr - load_offset);
  166. break;
  167. }
  168. if (!strtab)
  169. return -1;
  170. dynamic = (Elf64_Dyn *) (map + dyn_offset);
  171. while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
  172. Elf64_Dyn *curr = dynamic;
  173. dynamic++;
  174. if (curr->d_tag != DT_NEEDED)
  175. continue;
  176. if (add_path_and_deps(&strtab[curr->d_un.d_val], 1, -1, 1))
  177. return -1;
  178. }
  179. return 0;
  180. }
  181. int elf_load_deps(const char *path, const char *map)
  182. {
  183. unsigned long dyn_offset, dyn_size;
  184. unsigned long load_offset, load_vaddr;
  185. unsigned long interp_offset;
  186. if (elf_find_section(map, PT_INTERP, &interp_offset, NULL, NULL) == 0) {
  187. add_path_and_deps(map+interp_offset, 1, -1, 0);
  188. }
  189. if (elf_find_section(map, PT_LOAD, &load_offset, NULL, &load_vaddr)) {
  190. DEBUG("failed to load the .load section from %s\n", path);
  191. return 0;
  192. }
  193. if (elf_find_section(map, PT_DYNAMIC, &dyn_offset, &dyn_size, NULL)) {
  194. DEBUG("failed to load the .dynamic section from %s\n", path);
  195. return 0;
  196. }
  197. int clazz = map[EI_CLASS];
  198. if (clazz == ELFCLASS32)
  199. return elf32_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
  200. else if (clazz == ELFCLASS64)
  201. return elf64_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
  202. ERROR("unknown elf format %d\n", clazz);
  203. return -1;
  204. }
  205. static void load_ldso_conf(const char *conf)
  206. {
  207. FILE* fp = fopen(conf, "r");
  208. char line[PATH_MAX];
  209. if (!fp) {
  210. DEBUG("failed to open %s\n", conf);
  211. return;
  212. }
  213. while (!feof(fp)) {
  214. int len;
  215. if (!fgets(line, sizeof(line), fp))
  216. break;
  217. len = strlen(line);
  218. if (len < 2)
  219. continue;
  220. if (*line == '#')
  221. continue;
  222. if (line[len - 1] == '\n')
  223. line[len - 1] = '\0';
  224. if (!strncmp(line, "include ", 8)) {
  225. char *sep = strstr(line, " ");
  226. glob_t gl;
  227. int i;
  228. if (!sep)
  229. continue;;
  230. while (*sep == ' ')
  231. sep++;
  232. if (glob(sep, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
  233. ERROR("glob failed on %s\n", sep);
  234. continue;
  235. }
  236. for (i = 0; i < gl.gl_pathc; i++)
  237. load_ldso_conf(gl.gl_pathv[i]);
  238. globfree(&gl);
  239. } else {
  240. alloc_library_path(line);
  241. }
  242. }
  243. fclose(fp);
  244. }
  245. void init_library_search(void)
  246. {
  247. avl_init(&libraries, avl_strcmp, false, NULL);
  248. alloc_library_path("/lib");
  249. alloc_library_path("/lib64");
  250. alloc_library_path("/usr/lib");
  251. load_ldso_conf("/etc/ld.so.conf");
  252. }
  253. void free_library_search(void)
  254. {
  255. struct library_path *p, *ptmp;
  256. struct library *l, *tmp;
  257. list_for_each_entry_safe(p, ptmp, &library_paths, list)
  258. free(p);
  259. avl_remove_all_elements(&libraries, l, avl, tmp)
  260. free(l);
  261. }