1 /*-
2 * Copyright 1996-1998 John D. Polstra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD$
26 */
27
28 #include <sys/param.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31
32 #include <errno.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "debug.h"
39 #include "rtld.h"
40
41 static Elf_Ehdr *get_elf_header(int, const char *, const struct stat *,
42 Elf_Phdr **phdr);
43 static int convert_flags(int); /* Elf flags -> mmap flags */
44
45 int __getosreldate(void);
46
47 static bool
phdr_in_zero_page(const Elf_Ehdr * hdr)48 phdr_in_zero_page(const Elf_Ehdr *hdr)
49 {
50 return (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) <=
51 (size_t)PAGE_SIZE);
52 }
53
54 /*
55 * Map a shared object into memory. The "fd" argument is a file descriptor,
56 * which must be open on the object and positioned at its beginning.
57 * The "path" argument is a pathname that is used only for error messages.
58 *
59 * The return value is a pointer to a newly-allocated Obj_Entry structure
60 * for the shared object. Returns NULL on failure.
61 */
62 Obj_Entry *
map_object(int fd,const char * path,const struct stat * sb)63 map_object(int fd, const char *path, const struct stat *sb)
64 {
65 Obj_Entry *obj;
66 Elf_Ehdr *hdr;
67 int i;
68 Elf_Phdr *phdr;
69 Elf_Phdr *phlimit;
70 Elf_Phdr **segs;
71 int nsegs;
72 Elf_Phdr *phdyn;
73 Elf_Phdr *phinterp;
74 Elf_Phdr *phtls;
75 caddr_t mapbase;
76 size_t mapsize;
77 Elf_Addr base_vaddr;
78 Elf_Addr base_vlimit;
79 caddr_t base_addr;
80 int base_flags;
81 Elf_Off data_offset;
82 Elf_Addr data_vaddr;
83 Elf_Addr data_vlimit;
84 caddr_t data_addr;
85 int data_prot;
86 int data_flags;
87 Elf_Addr clear_vaddr;
88 caddr_t clear_addr;
89 caddr_t clear_page;
90 Elf_Addr phdr_vaddr;
91 size_t nclear, phsize;
92 Elf_Addr bss_vaddr;
93 Elf_Addr bss_vlimit;
94 caddr_t bss_addr;
95 Elf_Word stack_flags;
96 Elf_Addr relro_page;
97 size_t relro_size;
98 Elf_Addr note_start;
99 Elf_Addr note_end;
100 char *note_map;
101 size_t note_map_len;
102
103 hdr = get_elf_header(fd, path, sb, &phdr);
104 if (hdr == NULL)
105 return (NULL);
106
107 /*
108 * Scan the program header entries, and save key information.
109 * We expect that the loadable segments are ordered by load address.
110 */
111 phsize = hdr->e_phnum * sizeof(phdr[0]);
112 phlimit = phdr + hdr->e_phnum;
113 nsegs = -1;
114 phdyn = phinterp = phtls = NULL;
115 phdr_vaddr = 0;
116 relro_page = 0;
117 relro_size = 0;
118 note_start = 0;
119 note_end = 0;
120 note_map = NULL;
121 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
122 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
123 while (phdr < phlimit) {
124 switch (phdr->p_type) {
125
126 case PT_INTERP:
127 phinterp = phdr;
128 break;
129
130 case PT_LOAD:
131 segs[++nsegs] = phdr;
132 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
133 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
134 path, nsegs);
135 goto error;
136 }
137 break;
138
139 case PT_PHDR:
140 phdr_vaddr = phdr->p_vaddr;
141 phsize = phdr->p_memsz;
142 break;
143
144 case PT_DYNAMIC:
145 phdyn = phdr;
146 break;
147
148 case PT_TLS:
149 phtls = phdr;
150 break;
151
152 case PT_GNU_STACK:
153 stack_flags = phdr->p_flags;
154 break;
155
156 case PT_GNU_RELRO:
157 relro_page = phdr->p_vaddr;
158 relro_size = phdr->p_memsz;
159 break;
160
161 case PT_NOTE:
162 if (phdr->p_offset > PAGE_SIZE ||
163 phdr->p_offset + phdr->p_filesz > PAGE_SIZE) {
164 note_map_len = round_page(phdr->p_offset +
165 phdr->p_filesz) - trunc_page(phdr->p_offset);
166 note_map = mmap(NULL, note_map_len, PROT_READ,
167 MAP_PRIVATE, fd, trunc_page(phdr->p_offset));
168 if (note_map == MAP_FAILED) {
169 _rtld_error("%s: error mapping PT_NOTE (%d)", path, errno);
170 goto error;
171 }
172 note_start = (Elf_Addr)(note_map + phdr->p_offset -
173 trunc_page(phdr->p_offset));
174 } else {
175 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
176 }
177 note_end = note_start + phdr->p_filesz;
178 break;
179 }
180
181 ++phdr;
182 }
183 if (phdyn == NULL) {
184 _rtld_error("%s: object is not dynamically-linked", path);
185 goto error;
186 }
187
188 if (nsegs < 0) {
189 _rtld_error("%s: too few PT_LOAD segments", path);
190 goto error;
191 }
192
193 /*
194 * Map the entire address space of the object, to stake out our
195 * contiguous region, and to establish the base address for relocation.
196 */
197 base_vaddr = trunc_page(segs[0]->p_vaddr);
198 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
199 mapsize = base_vlimit - base_vaddr;
200 base_addr = (caddr_t) base_vaddr;
201 base_flags = __getosreldate() >= P_OSREL_MAP_GUARD ||
202 (P_OSREL_MAJOR(__getosreldate()) == 11 && __getosreldate() >=
203 P_OSREL_MAP_GUARD_11) ? MAP_GUARD : MAP_PRIVATE | MAP_ANON | MAP_NOCORE;
204 if (npagesizes > 1 && round_page(segs[0]->p_filesz) >= pagesizes[1])
205 base_flags |= MAP_ALIGNED_SUPER;
206 if (base_vaddr != 0)
207 base_flags |= MAP_FIXED | MAP_EXCL;
208
209 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
210 if (mapbase == (caddr_t) -1) {
211 _rtld_error("%s: mmap of entire address space failed: %s",
212 path, rtld_strerror(errno));
213 goto error;
214 }
215 if (base_addr != NULL && mapbase != base_addr) {
216 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
217 path, base_addr, mapbase);
218 goto error1;
219 }
220
221 for (i = 0; i <= nsegs; i++) {
222 /* Overlay the segment onto the proper region. */
223 data_offset = trunc_page(segs[i]->p_offset);
224 data_vaddr = trunc_page(segs[i]->p_vaddr);
225 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
226 data_addr = mapbase + (data_vaddr - base_vaddr);
227 data_prot = convert_prot(segs[i]->p_flags);
228 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
229 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
230 data_flags | MAP_PREFAULT_READ, fd, data_offset) == (caddr_t) -1) {
231 _rtld_error("%s: mmap of data failed: %s", path,
232 rtld_strerror(errno));
233 goto error1;
234 }
235
236 /* Do BSS setup */
237 if (segs[i]->p_filesz != segs[i]->p_memsz) {
238
239 /* Clear any BSS in the last page of the segment. */
240 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
241 clear_addr = mapbase + (clear_vaddr - base_vaddr);
242 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
243
244 if ((nclear = data_vlimit - clear_vaddr) > 0) {
245 /* Make sure the end of the segment is writable */
246 if ((data_prot & PROT_WRITE) == 0 && -1 ==
247 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
248 _rtld_error("%s: mprotect failed: %s", path,
249 rtld_strerror(errno));
250 goto error1;
251 }
252
253 memset(clear_addr, 0, nclear);
254
255 /* Reset the data protection back */
256 if ((data_prot & PROT_WRITE) == 0)
257 mprotect(clear_page, PAGE_SIZE, data_prot);
258 }
259
260 /* Overlay the BSS segment onto the proper region. */
261 bss_vaddr = data_vlimit;
262 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
263 bss_addr = mapbase + (bss_vaddr - base_vaddr);
264 if (bss_vlimit > bss_vaddr) { /* There is something to do */
265 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
266 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
267 _rtld_error("%s: mmap of bss failed: %s", path,
268 rtld_strerror(errno));
269 goto error1;
270 }
271 }
272 }
273
274 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
275 (data_vlimit - data_vaddr + data_offset) >=
276 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
277 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
278 }
279 }
280
281 obj = obj_new();
282 if (sb != NULL) {
283 obj->dev = sb->st_dev;
284 obj->ino = sb->st_ino;
285 }
286 obj->mapbase = mapbase;
287 obj->mapsize = mapsize;
288 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
289 base_vaddr;
290 obj->vaddrbase = base_vaddr;
291 obj->relocbase = mapbase - base_vaddr;
292 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
293 if (hdr->e_entry != 0)
294 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
295 if (phdr_vaddr != 0) {
296 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr);
297 } else {
298 obj->phdr = malloc(phsize);
299 if (obj->phdr == NULL) {
300 obj_free(obj);
301 _rtld_error("%s: cannot allocate program header", path);
302 goto error1;
303 }
304 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize);
305 obj->phdr_alloc = true;
306 }
307 obj->phsize = phsize;
308 if (phinterp != NULL)
309 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
310 if (phtls != NULL) {
311 tls_dtv_generation++;
312 obj->tlsindex = ++tls_max_index;
313 obj->tlssize = phtls->p_memsz;
314 obj->tlsalign = phtls->p_align;
315 obj->tlsinitsize = phtls->p_filesz;
316 obj->tlsinit = mapbase + phtls->p_vaddr;
317 }
318 obj->stack_flags = stack_flags;
319 obj->relro_page = obj->relocbase + trunc_page(relro_page);
320 obj->relro_size = round_page(relro_size);
321 if (note_start < note_end)
322 digest_notes(obj, note_start, note_end);
323 if (note_map != NULL)
324 munmap(note_map, note_map_len);
325 munmap(hdr, PAGE_SIZE);
326 return (obj);
327
328 error1:
329 munmap(mapbase, mapsize);
330 error:
331 if (note_map != NULL && note_map != MAP_FAILED)
332 munmap(note_map, note_map_len);
333 if (!phdr_in_zero_page(hdr))
334 munmap(phdr, hdr->e_phnum * sizeof(phdr[0]));
335 munmap(hdr, PAGE_SIZE);
336 return (NULL);
337 }
338
339 static Elf_Ehdr *
get_elf_header(int fd,const char * path,const struct stat * sbp,Elf_Phdr ** phdr_p)340 get_elf_header(int fd, const char *path, const struct stat *sbp,
341 Elf_Phdr **phdr_p)
342 {
343 Elf_Ehdr *hdr;
344 Elf_Phdr *phdr;
345
346 /* Make sure file has enough data for the ELF header */
347 if (sbp != NULL && sbp->st_size < sizeof(Elf_Ehdr)) {
348 _rtld_error("%s: invalid file format", path);
349 return (NULL);
350 }
351
352 hdr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
353 fd, 0);
354 if (hdr == (Elf_Ehdr *)MAP_FAILED) {
355 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
356 return (NULL);
357 }
358
359 /* Make sure the file is valid */
360 if (!IS_ELF(*hdr)) {
361 _rtld_error("%s: invalid file format", path);
362 goto error;
363 }
364 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
365 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
366 _rtld_error("%s: unsupported file layout", path);
367 goto error;
368 }
369 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
370 hdr->e_version != EV_CURRENT) {
371 _rtld_error("%s: unsupported file version", path);
372 goto error;
373 }
374 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
375 _rtld_error("%s: unsupported file type", path);
376 goto error;
377 }
378 if (hdr->e_machine != ELF_TARG_MACH) {
379 _rtld_error("%s: unsupported machine", path);
380 goto error;
381 }
382
383 /*
384 * We rely on the program header being in the first page. This is
385 * not strictly required by the ABI specification, but it seems to
386 * always true in practice. And, it simplifies things considerably.
387 */
388 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
389 _rtld_error(
390 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
391 goto error;
392 }
393 if (phdr_in_zero_page(hdr)) {
394 phdr = (Elf_Phdr *)((char *)hdr + hdr->e_phoff);
395 } else {
396 phdr = mmap(NULL, hdr->e_phnum * sizeof(phdr[0]),
397 PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ, fd,
398 hdr->e_phoff);
399 if (phdr == MAP_FAILED) {
400 _rtld_error("%s: error mapping phdr: %s", path,
401 rtld_strerror(errno));
402 goto error;
403 }
404 }
405 *phdr_p = phdr;
406 return (hdr);
407
408 error:
409 munmap(hdr, PAGE_SIZE);
410 return (NULL);
411 }
412
413 void
obj_free(Obj_Entry * obj)414 obj_free(Obj_Entry *obj)
415 {
416 Objlist_Entry *elm;
417
418 if (obj->tls_done)
419 free_tls_offset(obj);
420 while (obj->needed != NULL) {
421 Needed_Entry *needed = obj->needed;
422 obj->needed = needed->next;
423 free(needed);
424 }
425 while (!STAILQ_EMPTY(&obj->names)) {
426 Name_Entry *entry = STAILQ_FIRST(&obj->names);
427 STAILQ_REMOVE_HEAD(&obj->names, link);
428 free(entry);
429 }
430 while (!STAILQ_EMPTY(&obj->dldags)) {
431 elm = STAILQ_FIRST(&obj->dldags);
432 STAILQ_REMOVE_HEAD(&obj->dldags, link);
433 free(elm);
434 }
435 while (!STAILQ_EMPTY(&obj->dagmembers)) {
436 elm = STAILQ_FIRST(&obj->dagmembers);
437 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
438 free(elm);
439 }
440 if (obj->vertab)
441 free(obj->vertab);
442 if (obj->origin_path)
443 free(obj->origin_path);
444 if (obj->z_origin)
445 free(obj->rpath);
446 if (obj->priv)
447 free(obj->priv);
448 if (obj->path)
449 free(obj->path);
450 if (obj->phdr_alloc)
451 free((void *)obj->phdr);
452 free(obj);
453 }
454
455 Obj_Entry *
obj_new(void)456 obj_new(void)
457 {
458 Obj_Entry *obj;
459
460 obj = CNEW(Obj_Entry);
461 STAILQ_INIT(&obj->dldags);
462 STAILQ_INIT(&obj->dagmembers);
463 STAILQ_INIT(&obj->names);
464 return obj;
465 }
466
467 /*
468 * Given a set of ELF protection flags, return the corresponding protection
469 * flags for MMAP.
470 */
471 int
convert_prot(int elfflags)472 convert_prot(int elfflags)
473 {
474 int prot = 0;
475 if (elfflags & PF_R)
476 prot |= PROT_READ;
477 if (elfflags & PF_W)
478 prot |= PROT_WRITE;
479 if (elfflags & PF_X)
480 prot |= PROT_EXEC;
481 return prot;
482 }
483
484 static int
convert_flags(int elfflags)485 convert_flags(int elfflags)
486 {
487 int flags = MAP_PRIVATE; /* All mappings are private */
488
489 /*
490 * Readonly mappings are marked "MAP_NOCORE", because they can be
491 * reconstructed by a debugger.
492 */
493 if (!(elfflags & PF_W))
494 flags |= MAP_NOCORE;
495 return flags;
496 }
497