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.
26 #include <libubox/utils.h>
30 struct avl_tree libraries
;
31 static LIST_HEAD(library_paths
);
33 void alloc_library_path(const char *path
)
39 struct library_path
*p
;
42 p
= calloc_a(sizeof(*p
),
43 &_path
, strlen(path
) + 1);
47 p
->path
= strcpy(_path
, path
);
49 list_add_tail(&p
->list
, &library_paths
);
50 DEBUG("adding ld.so path %s\n", path
);
53 static void alloc_library(const char *path
, const char *name
)
58 l
= calloc_a(sizeof(*l
),
59 &_path
, strlen(path
) + 1,
60 &_name
, strlen(name
) + 1);
64 l
->avl
.key
= l
->name
= strcpy(_name
, name
);
65 l
->path
= strcpy(_path
, path
);
67 avl_insert(&libraries
, &l
->avl
);
68 DEBUG("adding library %s/%s\n", path
, name
);
71 static int elf_open(char **dir
, const char *file
)
73 struct library_path
*p
;
79 list_for_each_entry(p
, &library_paths
, list
) {
81 snprintf(path
, sizeof(path
), "%s/%s", p
->path
, file
);
83 strncpy(path
, file
, sizeof(path
));
84 fd
= open(path
, O_RDONLY
);
92 fd
= open(file
, O_RDONLY
);
97 const char* find_lib(const char *file
)
100 static char path
[256];
103 l
= avl_find_element(&libraries
, file
, l
, avl
);
108 if (strstr(p
, "local"))
111 snprintf(path
, sizeof(path
), "%s/%s", p
, file
);
116 static int elf64_find_section(const char *map
, unsigned int type
, unsigned int *offset
, unsigned int *size
, unsigned int *vaddr
)
122 e
= (Elf64_Ehdr
*) map
;
123 ph
= (Elf64_Phdr
*) (map
+ e
->e_phoff
);
125 for (i
= 0; i
< e
->e_phnum
; i
++) {
126 if (ph
[i
].p_type
== type
) {
127 *offset
= ph
[i
].p_offset
;
129 *size
= ph
[i
].p_filesz
;
131 *vaddr
= ph
[i
].p_vaddr
;
139 static int elf32_find_section(const char *map
, unsigned int type
, unsigned int *offset
, unsigned int *size
, unsigned int *vaddr
)
145 e
= (Elf32_Ehdr
*) map
;
146 ph
= (Elf32_Phdr
*) (map
+ e
->e_phoff
);
148 for (i
= 0; i
< e
->e_phnum
; i
++) {
149 if (ph
[i
].p_type
== type
) {
150 *offset
= ph
[i
].p_offset
;
152 *size
= ph
[i
].p_filesz
;
154 *vaddr
= ph
[i
].p_vaddr
;
162 static int elf_find_section(const char *map
, unsigned int type
, unsigned int *offset
, unsigned int *size
, unsigned int *vaddr
)
164 int clazz
= map
[EI_CLASS
];
166 if (clazz
== ELFCLASS32
)
167 return elf32_find_section(map
, type
, offset
, size
, vaddr
);
168 else if (clazz
== ELFCLASS64
)
169 return elf64_find_section(map
, type
, offset
, size
, vaddr
);
171 ERROR("unknown elf format %d\n", clazz
);
176 static int elf32_scan_dynamic(const char *map
, int dyn_offset
, int dyn_size
, int load_offset
)
178 Elf32_Dyn
*dynamic
= (Elf32_Dyn
*) (map
+ dyn_offset
);
179 const char *strtab
= NULL
;
181 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
182 Elf32_Dyn
*curr
= dynamic
;
185 if (curr
->d_tag
!= DT_STRTAB
)
188 strtab
= map
+ (curr
->d_un
.d_val
- load_offset
);
195 dynamic
= (Elf32_Dyn
*) (map
+ dyn_offset
);
196 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
197 Elf32_Dyn
*curr
= dynamic
;
200 if (curr
->d_tag
!= DT_NEEDED
)
203 if (elf_load_deps(&strtab
[curr
->d_un
.d_val
]))
210 static int elf64_scan_dynamic(const char *map
, int dyn_offset
, int dyn_size
, int load_offset
)
212 Elf64_Dyn
*dynamic
= (Elf64_Dyn
*) (map
+ dyn_offset
);
213 const char *strtab
= NULL
;
215 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
216 Elf64_Dyn
*curr
= dynamic
;
219 if (curr
->d_tag
!= DT_STRTAB
)
222 strtab
= map
+ (curr
->d_un
.d_val
- load_offset
);
229 dynamic
= (Elf64_Dyn
*) (map
+ dyn_offset
);
230 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
231 Elf64_Dyn
*curr
= dynamic
;
234 if (curr
->d_tag
!= DT_NEEDED
)
237 if (elf_load_deps(&strtab
[curr
->d_un
.d_val
]))
244 int elf_load_deps(const char *library
)
246 unsigned int dyn_offset
, dyn_size
;
247 unsigned int load_offset
, load_vaddr
;
249 char *map
= NULL
, *dir
= NULL
;
250 int clazz
, fd
, ret
= -1;
252 if (avl_find(&libraries
, library
))
255 fd
= elf_open(&dir
, library
);
258 ERROR("failed to open %s\n", library
);
262 if (fstat(fd
, &s
) == -1) {
263 ERROR("failed to stat %s\n", library
);
268 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
269 if (map
== MAP_FAILED
) {
270 ERROR("failed to mmap %s\n", library
);
275 if (elf_find_section(map
, PT_LOAD
, &load_offset
, NULL
, &load_vaddr
)) {
276 ERROR("failed to load the .load section from %s\n", library
);
281 if (elf_find_section(map
, PT_DYNAMIC
, &dyn_offset
, &dyn_size
, NULL
)) {
282 ERROR("failed to load the .dynamic section from %s\n", library
);
288 alloc_library(dir
, library
);
290 char *elf1
= strdup(library
);
291 char *elf2
= strdup(library
);
293 alloc_library(dirname(elf1
), basename(elf2
));
297 clazz
= map
[EI_CLASS
];
299 if (clazz
== ELFCLASS32
)
300 ret
= elf32_scan_dynamic(map
, dyn_offset
, dyn_size
, load_vaddr
- load_offset
);
301 else if (clazz
== ELFCLASS64
)
302 ret
= elf64_scan_dynamic(map
, dyn_offset
, dyn_size
, load_vaddr
- load_offset
);
306 munmap(map
, s
.st_size
);
312 void load_ldso_conf(const char *conf
)
314 FILE* fp
= fopen(conf
, "r");
318 DEBUG("failed to open %s\n", conf
);
325 if (!fgets(line
, 256, fp
))
332 if (line
[len
- 1] == '\n')
333 line
[len
- 1] = '\0';
334 if (!strncmp(line
, "include ", 8)) {
335 char *sep
= strstr(line
, " ");
343 if (glob(sep
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
)) {
344 ERROR("glob failed on %s\n", sep
);
347 for (i
= 0; i
< gl
.gl_pathc
; i
++)
348 load_ldso_conf(gl
.gl_pathv
[i
]);
351 alloc_library_path(line
);