2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
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
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.
27 #include <libubox/utils.h>
31 struct avl_tree libraries;
32 static LIST_HEAD(library_paths);
34 void alloc_library_path(const char *path)
40 struct library_path *p;
43 p = calloc_a(sizeof(*p),
44 &_path, strlen(path) + 1);
48 p->path = strcpy(_path, path);
50 list_add_tail(&p->list, &library_paths);
51 DEBUG("adding ld.so path %s\n", path);
54 static void alloc_library(const char *path, const char *name)
59 l = calloc_a(sizeof(*l),
60 &_path, strlen(path) + 1,
61 &_name, strlen(name) + 1);
65 l->avl.key = l->name = strcpy(_name, name);
66 l->path = strcpy(_path, path);
68 avl_insert(&libraries, &l->avl);
69 DEBUG("adding library %s/%s\n", path, name);
72 static int elf_open(char **dir, char *file)
74 struct library_path *p;
80 list_for_each_entry(p, &library_paths, list) {
82 snprintf(path, sizeof(path), "%s/%s", p->path, file);
84 strncpy(path, file, sizeof(path));
85 fd = open(path, O_RDONLY);
93 fd = open(file, O_RDONLY);
98 char* find_lib(char *file)
101 static char path[256];
104 l = avl_find_element(&libraries, file, l, avl);
109 if (strstr(p, "local"))
112 snprintf(path, sizeof(path), "%s/%s", p, file);
117 static int elf64_find_section(char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
123 e = (Elf64_Ehdr *) map;
124 ph = (Elf64_Phdr *) (map + e->e_phoff);
126 for (i = 0; i < e->e_phnum; i++) {
127 if (ph[i].p_type == type) {
128 *offset = ph[i].p_offset;
130 *size = ph[i].p_filesz;
132 *vaddr = ph[i].p_vaddr;
140 static int elf32_find_section(char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
146 e = (Elf32_Ehdr *) map;
147 ph = (Elf32_Phdr *) (map + e->e_phoff);
149 for (i = 0; i < e->e_phnum; i++) {
150 if (ph[i].p_type == type) {
151 *offset = ph[i].p_offset;
153 *size = ph[i].p_filesz;
155 *vaddr = ph[i].p_vaddr;
163 static int elf_find_section(char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
165 int clazz = map[EI_CLASS];
167 if (clazz == ELFCLASS32)
168 return elf32_find_section(map, type, offset, size, vaddr);
169 else if (clazz == ELFCLASS64)
170 return elf64_find_section(map, type, offset, size, vaddr);
172 ERROR("unknown elf format %d\n", clazz);
177 static int elf32_scan_dynamic(char *map, int dyn_offset, int dyn_size, int load_offset)
179 Elf32_Dyn *dynamic = (Elf32_Dyn *) (map + dyn_offset);
182 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
183 Elf32_Dyn *curr = dynamic;
186 if (curr->d_tag != DT_STRTAB)
189 strtab = map + (curr->d_un.d_val - load_offset);
196 dynamic = (Elf32_Dyn *) (map + dyn_offset);
197 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
198 Elf32_Dyn *curr = dynamic;
201 if (curr->d_tag != DT_NEEDED)
204 if (elf_load_deps(&strtab[curr->d_un.d_val]))
211 static int elf64_scan_dynamic(char *map, int dyn_offset, int dyn_size, int load_offset)
213 Elf64_Dyn *dynamic = (Elf64_Dyn *) (map + dyn_offset);
216 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
217 Elf64_Dyn *curr = dynamic;
220 if (curr->d_tag != DT_STRTAB)
223 strtab = map + (curr->d_un.d_val - load_offset);
230 dynamic = (Elf64_Dyn *) (map + dyn_offset);
231 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
232 Elf64_Dyn *curr = dynamic;
235 if (curr->d_tag != DT_NEEDED)
238 if (elf_load_deps(&strtab[curr->d_un.d_val]))
245 int elf_load_deps(char *library)
247 unsigned int dyn_offset, dyn_size;
248 unsigned int load_offset, load_vaddr;
250 char *map = NULL, *dir = NULL;
251 int clazz, fd, ret = -1;
253 if (avl_find(&libraries, library))
256 fd = elf_open(&dir, library);
259 ERROR("failed to open %s\n", library);
263 if (fstat(fd, &s) == -1) {
264 ERROR("failed to stat %s\n", library);
269 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
270 if (map == MAP_FAILED) {
271 ERROR("failed to mmap %s\n", library);
276 if (elf_find_section(map, PT_LOAD, &load_offset, NULL, &load_vaddr)) {
277 ERROR("failed to load the .load section from %s\n", library);
282 if (elf_find_section(map, PT_DYNAMIC, &dyn_offset, &dyn_size, NULL)) {
283 ERROR("failed to load the .dynamic section from %s\n", library);
289 alloc_library(dir, library);
291 char *elf = strdup(library);
293 alloc_library(dirname(elf), basename(library));
296 clazz = map[EI_CLASS];
298 if (clazz == ELFCLASS32)
299 ret = elf32_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
300 else if (clazz == ELFCLASS64)
301 ret = elf64_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
305 munmap(map, s.st_size);
311 void load_ldso_conf(const char *conf)
313 FILE* fp = fopen(conf, "r");
317 DEBUG("failed to open %s\n", conf);
324 if (!fgets(line, 256, fp))
331 if (line[len - 1] == '\n')
332 line[len - 1] = '\0';
333 if (!strncmp(line, "include ", 8)) {
334 char *sep = strstr(line, " ");
342 if (glob(sep, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
343 ERROR("glob failed on %s\n", sep);
346 for (i = 0; i < gl.gl_pathc; i++)
347 load_ldso_conf(gl.gl_pathv[i]);
350 alloc_library_path(line);