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.
15 #include <sys/syscall.h>
17 #include <sys/utsname.h>
18 #include <sys/types.h>
23 #include <sys/syscall.h>
24 #include <sys/types.h>
25 #include <sys/mount.h>
37 #include <libubox/avl.h>
38 #include <libubox/avl-cmp.h>
39 #include <libubox/utils.h>
40 #include <libubox/list.h>
41 #include <libubox/uloop.h>
45 struct avl_tree libraries
;
46 static LIST_HEAD(library_paths
);
48 void alloc_library_path(const char *path
)
50 struct library_path
*p
;
53 p
= calloc_a(sizeof(*p
),
54 &_path
, strlen(path
) + 1);
58 p
->path
= strcpy(_path
, path
);
60 list_add_tail(&p
->list
, &library_paths
);
61 DEBUG("adding ld.so path %s\n", path
);
64 static void alloc_library(const char *path
, const char *name
)
69 l
= calloc_a(sizeof(*l
),
70 &_path
, strlen(path
) + 1,
71 &_name
, strlen(name
) + 1);
75 l
->avl
.key
= l
->name
= strcpy(_name
, name
);
76 l
->path
= strcpy(_path
, path
);
78 avl_insert(&libraries
, &l
->avl
);
79 DEBUG("adding library %s/%s\n", path
, name
);
82 static int elf_open(char **dir
, char *file
)
84 struct library_path
*p
;
90 list_for_each_entry(p
, &library_paths
, list
) {
92 snprintf(path
, sizeof(path
), "%s/%s", p
->path
, file
);
94 strncpy(path
, file
, sizeof(path
));
95 fd
= open(path
, O_RDONLY
);
103 fd
= open(file
, O_RDONLY
);
108 char* find_lib(char *file
)
111 static char path
[256];
114 l
= avl_find_element(&libraries
, file
, l
, avl
);
119 if (strstr(p
, "local"))
122 snprintf(path
, sizeof(path
), "%s/%s", p
, file
);
127 static int elf64_find_section(char *map
, unsigned int type
, unsigned int *offset
, unsigned int *size
, unsigned int *vaddr
)
133 e
= (Elf64_Ehdr
*) map
;
134 ph
= (Elf64_Phdr
*) (map
+ e
->e_phoff
);
136 for (i
= 0; i
< e
->e_phnum
; i
++) {
137 if (ph
[i
].p_type
== type
) {
138 *offset
= ph
[i
].p_offset
;
140 *size
= ph
[i
].p_filesz
;
142 *vaddr
= ph
[i
].p_vaddr
;
150 static int elf32_find_section(char *map
, unsigned int type
, unsigned int *offset
, unsigned int *size
, unsigned int *vaddr
)
156 e
= (Elf32_Ehdr
*) map
;
157 ph
= (Elf32_Phdr
*) (map
+ e
->e_phoff
);
159 for (i
= 0; i
< e
->e_phnum
; i
++) {
160 if (ph
[i
].p_type
== type
) {
161 *offset
= ph
[i
].p_offset
;
163 *size
= ph
[i
].p_filesz
;
165 *vaddr
= ph
[i
].p_vaddr
;
173 static int elf_find_section(char *map
, unsigned int type
, unsigned int *offset
, unsigned int *size
, unsigned int *vaddr
)
175 int clazz
= map
[EI_CLASS
];
177 if (clazz
== ELFCLASS32
)
178 return elf32_find_section(map
, type
, offset
, size
, vaddr
);
179 else if (clazz
== ELFCLASS64
)
180 return elf64_find_section(map
, type
, offset
, size
, vaddr
);
182 ERROR("unknown elf format %d\n", clazz
);
187 static int elf32_scan_dynamic(char *map
, int dyn_offset
, int dyn_size
, int load_offset
)
189 Elf32_Dyn
*dynamic
= (Elf32_Dyn
*) (map
+ dyn_offset
);
192 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
193 Elf32_Dyn
*curr
= dynamic
;
196 if (curr
->d_tag
!= DT_STRTAB
)
199 strtab
= map
+ (curr
->d_un
.d_val
- load_offset
);
206 dynamic
= (Elf32_Dyn
*) (map
+ dyn_offset
);
207 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
208 Elf32_Dyn
*curr
= dynamic
;
211 if (curr
->d_tag
!= DT_NEEDED
)
214 if (elf_load_deps(&strtab
[curr
->d_un
.d_val
]))
221 static int elf64_scan_dynamic(char *map
, int dyn_offset
, int dyn_size
, int load_offset
)
223 Elf64_Dyn
*dynamic
= (Elf64_Dyn
*) (map
+ dyn_offset
);
226 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
227 Elf64_Dyn
*curr
= dynamic
;
230 if (curr
->d_tag
!= DT_STRTAB
)
233 strtab
= map
+ (curr
->d_un
.d_val
- load_offset
);
240 dynamic
= (Elf64_Dyn
*) (map
+ dyn_offset
);
241 while ((void *) dynamic
< (void *) (map
+ dyn_offset
+ dyn_size
)) {
242 Elf64_Dyn
*curr
= dynamic
;
245 if (curr
->d_tag
!= DT_NEEDED
)
248 if (elf_load_deps(&strtab
[curr
->d_un
.d_val
]))
255 int elf_load_deps(char *library
)
257 unsigned int dyn_offset
, dyn_size
;
258 unsigned int load_offset
, load_vaddr
;
260 char *map
= NULL
, *dir
= NULL
;
261 int clazz
, fd
, ret
= -1;
263 if (avl_find(&libraries
, library
))
266 fd
= elf_open(&dir
, library
);
269 ERROR("failed to open %s\n", library
);
273 if (fstat(fd
, &s
) == -1) {
274 ERROR("failed to stat %s\n", library
);
279 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
280 if (map
== MAP_FAILED
) {
281 ERROR("failed to mmap %s\n", library
);
286 if (elf_find_section(map
, PT_LOAD
, &load_offset
, NULL
, &load_vaddr
)) {
287 ERROR("failed to load the .load section from %s\n", library
);
292 if (elf_find_section(map
, PT_DYNAMIC
, &dyn_offset
, &dyn_size
, NULL
)) {
293 ERROR("failed to load the .dynamic section from %s\n", library
);
299 alloc_library(dir
, library
);
301 char *elf
= strdup(library
);
303 alloc_library(dirname(elf
), basename(library
));
306 clazz
= map
[EI_CLASS
];
308 if (clazz
== ELFCLASS32
)
309 ret
= elf32_scan_dynamic(map
, dyn_offset
, dyn_size
, load_vaddr
- load_offset
);
310 else if (clazz
== ELFCLASS64
)
311 ret
= elf64_scan_dynamic(map
, dyn_offset
, dyn_size
, load_vaddr
- load_offset
);
315 munmap(map
, s
.st_size
);
321 void load_ldso_conf(const char *conf
)
323 FILE* fp
= fopen(conf
, "r");
327 DEBUG("failed to open %s\n", conf
);
334 if (!fgets(line
, 256, fp
))
341 if (line
[len
- 1] == '\n')
342 line
[len
- 1] = '\0';
343 if (!strncmp(line
, "include ", 8)) {
344 char *sep
= strstr(line
, " ");
352 if (glob(sep
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
)) {
353 ERROR("glob failed on %s\n", sep
);
356 for (i
= 0; i
< gl
.gl_pathc
; i
++)
357 load_ldso_conf(gl
.gl_pathv
[i
]);
364 alloc_library_path(line
);