blob: cb35759bad6de15296c09143aff8e0ef3f20a74f [file] [log] [blame]
Rich Felker51e2d832011-06-18 19:48:42 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <stdint.h>
6#include <elf.h>
7#include <sys/mman.h>
8#include <limits.h>
9#include <stdint.h>
10#include <fcntl.h>
11#include <sys/stat.h>
12#include <errno.h>
13#include <limits.h>
14#include <elf.h>
15
16#include "reloc.h"
17
18#if ULONG_MAX == 0xffffffff
19typedef Elf32_Ehdr Ehdr;
20typedef Elf32_Phdr Phdr;
21typedef Elf32_Sym Sym;
22#define R_TYPE(x) ((x)&255)
23#define R_SYM(x) ((x)>>8)
24#else
25typedef Elf64_Ehdr Ehdr;
26typedef Elf64_Phdr Phdr;
27typedef Elf64_Sym Sym;
28#define R_TYPE(x) ((x)&0xffffffff)
29#define R_SYM(x) ((x)>>32)
30#endif
31
32struct dso
33{
34 struct dso *next, *prev;
35 int refcnt;
36 size_t *dynv;
37 Sym *syms;
Rich Felker596d60c2011-06-18 22:52:01 -040038 uint32_t *hashtab;
Rich Felker51e2d832011-06-18 19:48:42 -040039 char *strings;
40 unsigned char *base;
41 unsigned char *map;
42 size_t map_len;
43 dev_t dev;
44 ino_t ino;
45 int global;
46 int relocated;
47 char name[];
48};
49
50static struct dso *head, *tail, *libc;
Rich Felkerb7f6e0c2011-06-23 22:04:06 -040051static int trust_env;
Rich Felker51e2d832011-06-18 19:48:42 -040052
53#define AUX_CNT 15
54#define DYN_CNT 34
55
56static void decode_vec(size_t *v, size_t *a, size_t cnt)
57{
58 memset(a, 0, cnt*sizeof(size_t));
59 for (; v[0]; v+=2) if (v[0]<cnt) {
60 a[0] |= 1ULL<<v[0];
61 a[v[0]] = v[1];
62 }
63}
64
65static uint32_t hash(const char *s)
66{
67 uint_fast32_t h = 0;
68 while (*s) {
69 h = 16*h + *s++;
70 h ^= h>>24 & 0xf0;
71 }
72 return h & 0xfffffff;
73}
74
Rich Felker596d60c2011-06-18 22:52:01 -040075static Sym *lookup(const char *s, uint32_t h, Sym *syms, uint32_t *hashtab, char *strings)
Rich Felker51e2d832011-06-18 19:48:42 -040076{
77 size_t i;
78 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
79 if (!strcmp(s, strings+syms[i].st_name))
80 return syms+i;
81 }
82 return 0;
83}
84
85#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
86
87static void *find_sym(struct dso *dso, const char *s, int need_def)
88{
89 uint32_t h = hash(s);
90 for (; dso; dso=dso->next) {
91 Sym *sym = lookup(s, h, dso->syms, dso->hashtab, dso->strings);
92 if (sym && (!need_def || sym->st_shndx) && sym->st_value
93 && (1<<(sym->st_info&0xf) & OK_TYPES))
94 return dso->base + sym->st_value;
95 }
96 return 0;
97}
98
99static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t stride, Sym *syms, char *strings, struct dso *dso)
100{
101 Sym *sym;
102 const char *name;
103 size_t sym_val, sym_size;
104 size_t *reloc_addr;
105 void *ctx;
106 int type;
107 int sym_index;
108
109 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
110 reloc_addr = (void *)(base + rel[0]);
111 type = R_TYPE(rel[1]);
112 sym_index = R_SYM(rel[1]);
113 if (sym_index) {
114 sym = syms + sym_index;
115 name = strings + sym->st_name;
116 ctx = IS_COPY(type) ? dso->next : dso;
117 sym_val = (size_t)find_sym(ctx, name, 1);
118 sym_size = sym->st_size;
119 }
120 do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]);
121 }
122}
123
124static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
125{
126 size_t buf[896/sizeof(size_t)];
127 size_t phsize;
128 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
129 size_t this_min, this_max;
130 off_t off_start;
131 Ehdr *eh;
132 Phdr *ph;
133 unsigned prot;
134 unsigned char *map, *base;
135 size_t dyn;
136 size_t i;
137
138 ssize_t l = read(fd, buf, sizeof buf);
139 if (l<sizeof *eh) return 0;
140 eh = (void *)buf;
141 phsize = eh->e_phentsize * eh->e_phnum;
142 if (phsize + sizeof *eh > l) return 0;
143 if (eh->e_phoff + phsize > l) {
144 l = pread(fd, buf+sizeof *eh, phsize, eh->e_phoff);
145 if (l != phsize) return 0;
146 eh->e_phoff = sizeof *eh;
147 }
148 ph = (void *)((char *)buf + eh->e_phoff);
149 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
150 if (ph->p_type == PT_DYNAMIC)
151 dyn = ph->p_vaddr;
152 if (ph->p_type != PT_LOAD) continue;
153 if (ph->p_vaddr < addr_min) {
154 addr_min = ph->p_vaddr;
155 off_start = ph->p_offset;
156 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
157 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
158 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
159 }
160 if (ph->p_vaddr+ph->p_memsz > addr_max) {
161 addr_max = ph->p_vaddr+ph->p_memsz;
162 }
163 }
164 if (!dyn) return 0;
165 addr_max += PAGE_SIZE-1;
166 addr_max &= -PAGE_SIZE;
167 addr_min &= -PAGE_SIZE;
168 off_start &= -PAGE_SIZE;
169 map_len = addr_max - addr_min + off_start;
170 /* The first time, we map too much, possibly even more than
171 * the length of the file. This is okay because we will not
172 * use the invalid part; we just need to reserve the right
173 * amount of virtual address space to map over later. */
174 map = mmap(0, map_len, prot, MAP_PRIVATE, fd, off_start);
175 if (map==MAP_FAILED) return 0;
176 base = map - addr_min;
177 ph = (void *)((char *)buf + eh->e_phoff);
178 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
179 if (ph->p_type != PT_LOAD) continue;
180 /* Reuse the existing mapping for the lowest-address LOAD */
181 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
182 this_min = ph->p_vaddr & -PAGE_SIZE;
183 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
184 off_start = ph->p_offset & -PAGE_SIZE;
185 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
186 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
187 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
188 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED) {
189 munmap(map, map_len);
190 return 0;
191 }
192 if (ph->p_memsz > ph->p_filesz) {
193 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
194 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
195 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
196 if (pgbrk-(size_t)base < this_max && mmap((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) {
197 munmap(map, map_len);
198 return 0;
199 }
200 }
201 }
202 *lenp = map_len;
203 *basep = base;
204 *dynp = dyn;
205 return map;
206}
207
208static struct dso *load_library(const char *name)
209{
210 unsigned char *base, *map;
211 size_t dyno, map_len;
212 struct dso *p;
213 size_t dyn[DYN_CNT] = {0};
214 int fd;
215 struct stat st;
216
217 /* Catch and block attempts to reload the implementation itself */
218 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
219 static const char *rp, reserved[] =
220 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
221 char *z = strchr(name, '.');
222 if (z) {
223 size_t l = z-name;
224 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
225 if (*rp) {
226 if (!libc->prev) {
227 tail->next = libc;
228 libc->prev = tail;
229 tail = libc;
230 }
231 return libc;
232 }
233 }
234 }
235 /* Search for the name to see if it's already loaded */
236 for (p=head->next; p; p=p->next) {
237 if (!strcmp(p->name, name)) {
238 p->refcnt++;
239 return p;
240 }
241 }
242 if (name[0] == '/') {
243 fd = open(name, O_RDONLY);
244 } else {
245 static const char path[] = "/lib/\0/usr/local/lib/\0/usr/lib/\0";
246 const char *s;
247 char buf[NAME_MAX+32];
248 if (strlen(name) > NAME_MAX || strchr(name, '/')) return 0;
249 for (s=path; *s; s+=strlen(s)+1) {
250 strcpy(buf, s);
251 strcat(buf, name);
252 if ((fd = open(buf, O_RDONLY))>=0) break;
253 }
254 }
255 if (fd < 0) return 0;
256 if (fstat(fd, &st) < 0) {
257 close(fd);
258 return 0;
259 }
260 for (p=head->next; p; p=p->next) {
261 if (p->dev == st.st_dev && p->ino == st.st_ino) {
262 close(fd);
263 p->refcnt++;
264 return p;
265 }
266 }
267 map = map_library(fd, &map_len, &base, &dyno);
268 close(fd);
269 if (!map) return 0;
270 p = calloc(1, sizeof *p + strlen(name) + 1);
271 if (!p) {
272 munmap(map, map_len);
273 return 0;
274 }
275
276 p->map = map;
277 p->map_len = map_len;
278 p->base = base;
279 p->dynv = (void *)(base + dyno);
280 decode_vec(p->dynv, dyn, DYN_CNT);
281
282 p->syms = (void *)(base + dyn[DT_SYMTAB]);
283 p->hashtab = (void *)(base + dyn[DT_HASH]);
284 p->strings = (void *)(base + dyn[DT_STRTAB]);
285 p->dev = st.st_dev;
286 p->ino = st.st_ino;
287 p->global = 1;
288 p->refcnt = 1;
289 strcpy(p->name, name);
290
291 tail->next = p;
292 p->prev = tail;
293 tail = p;
294
295 return p;
296}
297
298static void load_deps(struct dso *p)
299{
300 size_t i;
301 for (; p; p=p->next) {
302 for (i=0; p->dynv[i]; i+=2) {
303 if (p->dynv[i] != DT_NEEDED) continue;
304 load_library(p->strings + p->dynv[i+1]);
305 }
306 }
307}
308
309static void reloc_all(struct dso *p)
310{
311 size_t dyn[DYN_CNT] = {0};
312 for (; p; p=p->next) {
313 if (p->relocated) continue;
314 decode_vec(p->dynv, dyn, DYN_CNT);
315 do_relocs(p->base, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
316 2+(dyn[DT_PLTREL]==DT_RELA), p->syms, p->strings, p);
317 do_relocs(p->base, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ],
318 2, p->syms, p->strings, p);
319 do_relocs(p->base, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ],
320 3, p->syms, p->strings, p);
Rich Felker368ba4a2011-06-25 00:18:19 -0400321 p->relocated = 1;
Rich Felker51e2d832011-06-18 19:48:42 -0400322 }
323}
324
325void *__dynlink(int argc, char **argv, size_t *got)
326{
327 size_t *auxv, aux[AUX_CNT] = {0};
328 size_t app_dyn[DYN_CNT] = {0};
329 size_t lib_dyn[DYN_CNT] = {0};
330 size_t i;
331 Phdr *phdr;
332 struct dso lib, app;
333
334 /* Find aux vector just past environ[] */
335 for (i=argc+1; argv[i]; i++);
336 auxv = (void *)(argv+i+1);
337
338 decode_vec(auxv, aux, AUX_CNT);
339
340 /* Relocate ldso's DYNAMIC pointer and load vector */
341 decode_vec((void *)(got[0] += aux[AT_BASE]), lib_dyn, DYN_CNT);
342
343 /* Find the program image's DYNAMIC section and decode it */
344 phdr = (void *)aux[AT_PHDR];
345 for (i=aux[AT_PHNUM]; i--; phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
346 if (phdr->p_type == PT_DYNAMIC) {
347 decode_vec((void *)phdr->p_vaddr, app_dyn, DYN_CNT);
348 break;
349 }
350 }
351
352 app = (struct dso){
353 .base = 0,
354 .strings = (void *)(app_dyn[DT_STRTAB]),
355 .hashtab = (void *)(app_dyn[DT_HASH]),
356 .syms = (void *)(app_dyn[DT_SYMTAB]),
357 .dynv = (void *)(phdr->p_vaddr),
358 .next = &lib
359 };
360
361 lib = (struct dso){
362 .base = (void *)aux[AT_BASE],
363 .strings = (void *)(aux[AT_BASE]+lib_dyn[DT_STRTAB]),
364 .hashtab = (void *)(aux[AT_BASE]+lib_dyn[DT_HASH]),
365 .syms = (void *)(aux[AT_BASE]+lib_dyn[DT_SYMTAB]),
366 .dynv = (void *)(got[0]),
367 .relocated = 1
368 };
369
370 /* Relocate the dynamic linker/libc */
371 do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_REL]),
372 lib_dyn[DT_RELSZ], 2, lib.syms, lib.strings, &app);
373 do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_RELA]),
374 lib_dyn[DT_RELASZ], 3, lib.syms, lib.strings, &app);
375
376 /* At this point the standard library is fully functional */
377
Rich Felkerb7f6e0c2011-06-23 22:04:06 -0400378 /* Only trust user/env if kernel says we're not suid/sgid */
379 trust_env = (aux[0]&0x7800)==0x7800
380 && aux[AT_UID]==aux[AT_EUID]
381 && aux[AT_GID]==aux[AT_EGID];
382
Rich Felker51e2d832011-06-18 19:48:42 -0400383 head = tail = &app;
384 libc = &lib;
385 app.next = 0;
386 load_deps(head);
387
388 reloc_all(head);
389
390 errno = 0;
391 return (void *)aux[AT_ENTRY];
392}