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.
25 #include <linux/limits.h>
27 #include <libubox/utils.h>
32 struct avl_tree libraries;
33 static LIST_HEAD(library_paths);
35 static void alloc_library_path(const char *path)
41 struct library_path *p;
44 p = calloc_a(sizeof(*p),
45 &_path, strlen(path) + 1);
49 p->path = strcpy(_path, path);
51 list_add_tail(&p->list, &library_paths);
52 DEBUG("adding ld.so path %s\n", path);
55 static void alloc_library(const char *path, const char *name)
60 l = calloc_a(sizeof(*l),
61 &_path, strlen(path) + 1,
62 &_name, strlen(name) + 1);
66 l->avl.key = l->name = strcpy(_name, name);
67 l->path = strcpy(_path, path);
69 avl_insert(&libraries, &l->avl);
70 DEBUG("adding library %s/%s\n", path, name);
73 static int elf_open(char **dir, const char *file)
75 struct library_path *p;
81 list_for_each_entry(p, &library_paths, list) {
83 snprintf(path, sizeof(path), "%s/%s", p->path, file);
85 strncpy(path, file, sizeof(path));
86 fd = open(path, O_RDONLY);
94 fd = open(file, O_RDONLY);
99 const char* find_lib(const char *file)
102 static char path[PATH_MAX];
104 l = avl_find_element(&libraries, file, l, avl);
108 snprintf(path, sizeof(path), "%s/%s", l->path, file);
113 static int elf64_find_section(const 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(const 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(const 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(const char *map, int dyn_offset, int dyn_size, int load_offset)
175 Elf32_Dyn *dynamic = (Elf32_Dyn *) (map + dyn_offset);
176 const char *strtab = NULL;
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_ptr - 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(const char *map, int dyn_offset, int dyn_size, int load_offset)
209 Elf64_Dyn *dynamic = (Elf64_Dyn *) (map + dyn_offset);
210 const char *strtab = NULL;
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_ptr - 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(const 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 *elf1 = strdup(library);
288 char *elf2 = strdup(library);
290 alloc_library(dirname(elf1), basename(elf2));
294 clazz = map[EI_CLASS];
296 if (clazz == ELFCLASS32)
297 ret = elf32_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
298 else if (clazz == ELFCLASS64)
299 ret = elf64_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
303 munmap(map, s.st_size);
309 static void load_ldso_conf(const char *conf)
311 FILE* fp = fopen(conf, "r");
315 DEBUG("failed to open %s\n", conf);
322 if (!fgets(line, sizeof(line), fp))
329 if (line[len - 1] == '\n')
330 line[len - 1] = '\0';
331 if (!strncmp(line, "include ", 8)) {
332 char *sep = strstr(line, " ");
340 if (glob(sep, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
341 ERROR("glob failed on %s\n", sep);
344 for (i = 0; i < gl.gl_pathc; i++)
345 load_ldso_conf(gl.gl_pathv[i]);
348 alloc_library_path(line);
355 void init_library_search(void)
357 avl_init(&libraries, avl_strcmp, false, NULL);
358 alloc_library_path("/lib");
359 alloc_library_path("/lib64");
360 alloc_library_path("/usr/lib");
361 load_ldso_conf("/etc/ld.so.conf");