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
];
105 l
= avl_find_element(&libraries
, file
, l
, avl
);
110 if (strstr(p
, "local"))
113 snprintf(path
, sizeof(path
), "%s/%s", p
, file
);
118 static int elf64_find_section(const char *map
, unsigned int type
, unsigned int *offset
, unsigned int *size
, unsigned int *vaddr
)
124 e
= (Elf64_Ehdr
*) map
;
125 ph
= (Elf64_Phdr
*) (map
+ e
->e_phoff
);
127 for (i
= 0; i
< e
->e_phnum
; i
++) {
128 if (ph
[i
].p_type
== type
) {
129 *offset
= ph
[i
].p_offset
;
131 *size
= ph
[i
].p_filesz
;
133 *vaddr
= ph
[i
].p_vaddr
;
141 static int elf32_find_section(const char *map
, unsigned int type
, unsigned int *offset
, unsigned int *size
, unsigned int *vaddr
)
147 e
= (Elf32_Ehdr
*) map
;
148 ph
= (Elf32_Phdr
*) (map
+ e
->e_phoff
);
150 for (i
= 0; i
< e
->e_phnum
; i
++) {
151 if (ph
[i
].p_type
== type
) {
152 *offset
= ph
[i
].p_offset
;
154 *size
= ph
[i
].p_filesz
;
156 *vaddr
= ph
[i
].p_vaddr
;
164 static int elf_find_section(const char *map
, unsigned int type
, unsigned int *offset
, unsigned int *size
, unsigned int *vaddr
)
166 int clazz
= map
[EI_CLASS
];
168 if (clazz
== ELFCLASS32
)
169 return elf32_find_section(map
, type
, offset
, size
, vaddr
);
170 else if (clazz
== ELFCLASS64
)
171 return elf64_find_section(map
, type
, offset
, size
, vaddr
);
173 ERROR("unknown elf format %d\n", clazz
);
178 static int elf32_scan_dynamic(const char *map
, int dyn_offset
, int dyn_size
, int load_offset
)
180 Elf32_Dyn
*dynamic
= (Elf32_Dyn
*) (map
+ dyn_offset
);
181 const char *strtab
= NULL
;
183 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
184 Elf32_Dyn
*curr
= dynamic
;
187 if (curr
->d_tag
!= DT_STRTAB
)
190 strtab
= map
+ (curr
->d_un
.d_val
- load_offset
);
197 dynamic
= (Elf32_Dyn
*) (map
+ dyn_offset
);
198 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
199 Elf32_Dyn
*curr
= dynamic
;
202 if (curr
->d_tag
!= DT_NEEDED
)
205 if (elf_load_deps(&strtab
[curr
->d_un
.d_val
]))
212 static int elf64_scan_dynamic(const char *map
, int dyn_offset
, int dyn_size
, int load_offset
)
214 Elf64_Dyn
*dynamic
= (Elf64_Dyn
*) (map
+ dyn_offset
);
215 const char *strtab
= NULL
;
217 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
218 Elf64_Dyn
*curr
= dynamic
;
221 if (curr
->d_tag
!= DT_STRTAB
)
224 strtab
= map
+ (curr
->d_un
.d_val
- load_offset
);
231 dynamic
= (Elf64_Dyn
*) (map
+ dyn_offset
);
232 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
233 Elf64_Dyn
*curr
= dynamic
;
236 if (curr
->d_tag
!= DT_NEEDED
)
239 if (elf_load_deps(&strtab
[curr
->d_un
.d_val
]))
246 int elf_load_deps(const char *library
)
248 unsigned int dyn_offset
, dyn_size
;
249 unsigned int load_offset
, load_vaddr
;
251 char *map
= NULL
, *dir
= NULL
;
252 int clazz
, fd
, ret
= -1;
254 if (avl_find(&libraries
, library
))
257 fd
= elf_open(&dir
, library
);
260 ERROR("failed to open %s\n", library
);
264 if (fstat(fd
, &s
) == -1) {
265 ERROR("failed to stat %s\n", library
);
270 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
271 if (map
== MAP_FAILED
) {
272 ERROR("failed to mmap %s\n", library
);
277 if (elf_find_section(map
, PT_LOAD
, &load_offset
, NULL
, &load_vaddr
)) {
278 ERROR("failed to load the .load section from %s\n", library
);
283 if (elf_find_section(map
, PT_DYNAMIC
, &dyn_offset
, &dyn_size
, NULL
)) {
284 ERROR("failed to load the .dynamic section from %s\n", library
);
290 alloc_library(dir
, library
);
292 char *elf1
= strdup(library
);
293 char *elf2
= strdup(library
);
295 alloc_library(dirname(elf1
), basename(elf2
));
299 clazz
= map
[EI_CLASS
];
301 if (clazz
== ELFCLASS32
)
302 ret
= elf32_scan_dynamic(map
, dyn_offset
, dyn_size
, load_vaddr
- load_offset
);
303 else if (clazz
== ELFCLASS64
)
304 ret
= elf64_scan_dynamic(map
, dyn_offset
, dyn_size
, load_vaddr
- load_offset
);
308 munmap(map
, s
.st_size
);
314 static void load_ldso_conf(const char *conf
)
316 FILE* fp
= fopen(conf
, "r");
320 DEBUG("failed to open %s\n", conf
);
327 if (!fgets(line
, sizeof(line
), fp
))
334 if (line
[len
- 1] == '\n')
335 line
[len
- 1] = '\0';
336 if (!strncmp(line
, "include ", 8)) {
337 char *sep
= strstr(line
, " ");
345 if (glob(sep
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
)) {
346 ERROR("glob failed on %s\n", sep
);
349 for (i
= 0; i
< gl
.gl_pathc
; i
++)
350 load_ldso_conf(gl
.gl_pathv
[i
]);
353 alloc_library_path(line
);
360 void init_library_search(void)
362 avl_init(&libraries
, avl_strcmp
, false, NULL
);
363 alloc_library_path("/lib");
364 alloc_library_path("/lib64");
365 alloc_library_path("/usr/lib");
366 load_ldso_conf("/etc/ld.so.conf");