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.
21 #include <linux/limits.h>
23 #include <libubox/utils.h>
29 struct avl_tree libraries;
30 static LIST_HEAD(library_paths);
32 static void alloc_library_path(const char *path)
38 struct library_path *p;
41 p = calloc_a(sizeof(*p),
42 &_path, strlen(path) + 1);
46 p->path = strcpy(_path, path);
48 list_add_tail(&p->list, &library_paths);
49 DEBUG("adding ld.so path %s\n", path);
54 * name = soname/avl key
56 void alloc_library(const char *path, const char *name)
61 l = calloc_a(sizeof(*l),
62 &_path, strlen(path) + 1,
63 &_name, strlen(name) + 1);
67 l->avl.key = l->name = strcpy(_name, name);
68 l->path = strcpy(_path, path);
70 avl_insert(&libraries, &l->avl);
71 DEBUG("adding library %s (%s)\n", path, name);
74 int lib_open(char **fullpath, const char *file)
76 struct library_path *p;
82 list_for_each_entry(p, &library_paths, list) {
83 snprintf(path, sizeof(path), "%s/%s", p->path, file);
84 fd = open(path, O_RDONLY|O_CLOEXEC);
86 *fullpath = strdup(path);
94 const char* find_lib(const char *file)
98 l = avl_find_element(&libraries, file, l, avl);
105 static int elf64_find_section(const char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
111 e = (Elf64_Ehdr *) map;
112 ph = (Elf64_Phdr *) (map + e->e_phoff);
114 for (i = 0; i < e->e_phnum; i++) {
115 if (ph[i].p_type == type) {
116 *offset = ph[i].p_offset;
118 *size = ph[i].p_filesz;
120 *vaddr = ph[i].p_vaddr;
128 static int elf32_find_section(const char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
134 e = (Elf32_Ehdr *) map;
135 ph = (Elf32_Phdr *) (map + e->e_phoff);
137 for (i = 0; i < e->e_phnum; i++) {
138 if (ph[i].p_type == type) {
139 *offset = ph[i].p_offset;
141 *size = ph[i].p_filesz;
143 *vaddr = ph[i].p_vaddr;
151 static int elf_find_section(const char *map, unsigned int type, unsigned int *offset, unsigned int *size, unsigned int *vaddr)
153 int clazz = map[EI_CLASS];
155 if (clazz == ELFCLASS32)
156 return elf32_find_section(map, type, offset, size, vaddr);
157 else if (clazz == ELFCLASS64)
158 return elf64_find_section(map, type, offset, size, vaddr);
160 ERROR("unknown elf format %d\n", clazz);
165 static int elf32_scan_dynamic(const char *map, int dyn_offset, int dyn_size, int load_offset)
167 Elf32_Dyn *dynamic = (Elf32_Dyn *) (map + dyn_offset);
168 const char *strtab = NULL;
170 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
171 Elf32_Dyn *curr = dynamic;
174 if (curr->d_tag != DT_STRTAB)
177 strtab = map + (curr->d_un.d_ptr - load_offset);
184 dynamic = (Elf32_Dyn *) (map + dyn_offset);
185 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
186 Elf32_Dyn *curr = dynamic;
189 if (curr->d_tag != DT_NEEDED)
192 if (add_path_and_deps(&strtab[curr->d_un.d_val], 1, -1, 1))
199 static int elf64_scan_dynamic(const char *map, int dyn_offset, int dyn_size, int load_offset)
201 Elf64_Dyn *dynamic = (Elf64_Dyn *) (map + dyn_offset);
202 const char *strtab = NULL;
204 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
205 Elf64_Dyn *curr = dynamic;
208 if (curr->d_tag != DT_STRTAB)
211 strtab = map + (curr->d_un.d_ptr - load_offset);
218 dynamic = (Elf64_Dyn *) (map + dyn_offset);
219 while ((void *) dynamic < (void *) (map + dyn_offset + dyn_size)) {
220 Elf64_Dyn *curr = dynamic;
223 if (curr->d_tag != DT_NEEDED)
226 if (add_path_and_deps(&strtab[curr->d_un.d_val], 1, -1, 1))
233 int elf_load_deps(const char *path, const char *map)
235 unsigned int dyn_offset, dyn_size;
236 unsigned int load_offset, load_vaddr;
237 unsigned int interp_offset;
239 if (elf_find_section(map, PT_LOAD, &load_offset, NULL, &load_vaddr)) {
240 ERROR("failed to load the .load section from %s\n", path);
244 if (elf_find_section(map, PT_DYNAMIC, &dyn_offset, &dyn_size, NULL)) {
245 ERROR("failed to load the .dynamic section from %s\n", path);
249 if (elf_find_section(map, PT_INTERP, &interp_offset, NULL, NULL) == 0) {
250 add_path_and_deps(map+interp_offset, 1, -1, 0);
253 int clazz = map[EI_CLASS];
255 if (clazz == ELFCLASS32)
256 return elf32_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
257 else if (clazz == ELFCLASS64)
258 return elf64_scan_dynamic(map, dyn_offset, dyn_size, load_vaddr - load_offset);
260 ERROR("unknown elf format %d\n", clazz);
264 static void load_ldso_conf(const char *conf)
266 FILE* fp = fopen(conf, "r");
270 DEBUG("failed to open %s\n", conf);
277 if (!fgets(line, sizeof(line), fp))
284 if (line[len - 1] == '\n')
285 line[len - 1] = '\0';
286 if (!strncmp(line, "include ", 8)) {
287 char *sep = strstr(line, " ");
295 if (glob(sep, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
296 ERROR("glob failed on %s\n", sep);
299 for (i = 0; i < gl.gl_pathc; i++)
300 load_ldso_conf(gl.gl_pathv[i]);
303 alloc_library_path(line);
310 void init_library_search(void)
312 avl_init(&libraries, avl_strcmp, false, NULL);
313 alloc_library_path("/lib");
314 alloc_library_path("/lib64");
315 alloc_library_path("/usr/lib");
316 load_ldso_conf("/etc/ld.so.conf");