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)
36 struct library_path *p;
39 p = calloc_a(sizeof(*p),
40 &_path, strlen(path) + 1);
44 p->path = strcpy(_path, path);
46 list_add_tail(&p->list, &library_paths);
47 DEBUG("adding ld.so path %s\n", path);
50 static void alloc_library(const char *path, const char *name)
55 l = calloc_a(sizeof(*l),
56 &_path, strlen(path) + 1,
57 &_name, strlen(name) + 1);
61 l->avl.key = l->name = strcpy(_name, name);
62 l->path = strcpy(_path, path);
64 avl_insert(&libraries, &l->avl);
65 DEBUG("adding library %s/%s\n", path, name);
68 static int elf_open(char **dir, char *file)
70 struct library_path *p;
76 list_for_each_entry(p, &library_paths, list) {
78 snprintf(path, sizeof(path), "%s/%s", p->path, file);
80 strncpy(path, file, sizeof(path));
81 fd = open(path, O_RDONLY);
89 fd = open(file, O_RDONLY);
94 char* find_lib(char *file)
97 static char path[256];
100 l = avl_find_element(&libraries, file, l, avl);
105 if (strstr(p, "local"))
108 snprintf(path, sizeof(path), "%s/%s", p, file);
113 static int elf64_find_section(char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
119 e = (Elf64_Ehdr *) map;
120 ph = (Elf64_Phdr *) (map + e->e_phoff);
122 for (i = 0; i < e->e_phnum; i++) {
123 if (ph[i].p_type == type) {
124 *offset = ph[i].p_offset;
126 *size = ph[i].p_filesz;
128 *vaddr = ph[i].p_vaddr;
136 static int elf32_find_section(char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
142 e = (Elf32_Ehdr *) map;
143 ph = (Elf32_Phdr *) (map + e->e_phoff);
145 for (i = 0; i < e->e_phnum; i++) {
146 if (ph[i].p_type == type) {
147 *offset = ph[i].p_offset;
149 *size = ph[i].p_filesz;
151 *vaddr = ph[i].p_vaddr;
159 static int elf_find_section(char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
161 int clazz = map[EI_CLASS];
163 if (clazz == ELFCLASS32)
164 return elf32_find_section(map, type, offset, size, vaddr);
165 else if (clazz == ELFCLASS64)
166 return elf64_find_section(map, type, offset, size, vaddr);
168 ERROR("unknown elf format %d\n", clazz);
173 static int elf32_scan_dynamic(char *map, int dyn_offset, int dyn_size, int load_offset)
175 Elf32_Dyn *dynamic = (Elf32_Dyn *) (map + dyn_offset);
178 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
179 Elf32_Dyn *curr = dynamic;
182 if (curr->d_tag != DT_STRTAB)
185 strtab = map + (curr->d_un.d_val - load_offset);
192 dynamic = (Elf32_Dyn *) (map + dyn_offset);
193 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
194 Elf32_Dyn *curr = dynamic;
197 if (curr->d_tag != DT_NEEDED)
200 if (elf_load_deps(&strtab[curr->d_un.d_val]))
207 static int elf64_scan_dynamic(char *map, int dyn_offset, int dyn_size, int load_offset)
209 Elf64_Dyn *dynamic = (Elf64_Dyn *) (map + dyn_offset);
212 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
213 Elf64_Dyn *curr = dynamic;
216 if (curr->d_tag != DT_STRTAB)
219 strtab = map + (curr->d_un.d_val - load_offset);
226 dynamic = (Elf64_Dyn *) (map + dyn_offset);
227 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
228 Elf64_Dyn *curr = dynamic;
231 if (curr->d_tag != DT_NEEDED)
234 if (elf_load_deps(&strtab[curr->d_un.d_val]))
241 int elf_load_deps(char *library)
243 unsigned int dyn_offset, dyn_size;
244 unsigned int load_offset, load_vaddr;
246 char *map = NULL, *dir = NULL;
247 int clazz, fd, ret = -1;
249 if (avl_find(&libraries, library))
252 fd = elf_open(&dir, library);
255 ERROR("failed to open %s\n", library);
259 if (fstat(fd, &s) == -1) {
260 ERROR("failed to stat %s\n", library);
265 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
266 if (map == MAP_FAILED) {
267 ERROR("failed to mmap %s\n", library);
272 if (elf_find_section(map, PT_LOAD, &load_offset, NULL, &load_vaddr)) {
273 ERROR("failed to load the .load section from %s\n", library);
278 if (elf_find_section(map, PT_DYNAMIC, &dyn_offset, &dyn_size, NULL)) {
279 ERROR("failed to load the .dynamic section from %s\n", library);
285 alloc_library(dir, library);
287 char *elf = strdup(library);
289 alloc_library(dirname(elf), basename(library));
292 clazz = map[EI_CLASS];
294 if (clazz == ELFCLASS32)
295 ret = elf32_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
296 else if (clazz == ELFCLASS64)
297 ret = elf64_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
301 munmap(map, s.st_size);
307 void load_ldso_conf(const char *conf)
309 FILE* fp = fopen(conf, "r");
313 DEBUG("failed to open %s\n", conf);
320 if (!fgets(line, 256, fp))
327 if (line[len - 1] == '\n')
328 line[len - 1] = '\0';
329 if (!strncmp(line, "include ", 8)) {
330 char *sep = strstr(line, " ");
338 if (glob(sep, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
339 ERROR("glob failed on %s\n", sep);
342 for (i = 0; i < gl.gl_pathc; i++)
343 load_ldso_conf(gl.gl_pathv[i]);
350 alloc_library_path(line);