]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/map_object.c
cdn-patch: offer option to mount /etc/keys before attaching geli devices
[FreeBSD/FreeBSD.git] / libexec / rtld-elf / map_object.c
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 static int convert_flags(int); /* Elf flags -> mmap flags */
43
44 int __getosreldate(void);
45
46 /*
47  * Map a shared object into memory.  The "fd" argument is a file descriptor,
48  * which must be open on the object and positioned at its beginning.
49  * The "path" argument is a pathname that is used only for error messages.
50  *
51  * The return value is a pointer to a newly-allocated Obj_Entry structure
52  * for the shared object.  Returns NULL on failure.
53  */
54 Obj_Entry *
55 map_object(int fd, const char *path, const struct stat *sb)
56 {
57     Obj_Entry *obj;
58     Elf_Ehdr *hdr;
59     int i;
60     Elf_Phdr *phdr;
61     Elf_Phdr *phlimit;
62     Elf_Phdr **segs;
63     int nsegs;
64     Elf_Phdr *phdyn;
65     Elf_Phdr *phinterp;
66     Elf_Phdr *phtls;
67     caddr_t mapbase;
68     size_t mapsize;
69     Elf_Addr base_vaddr;
70     Elf_Addr base_vlimit;
71     caddr_t base_addr;
72     int base_flags;
73     Elf_Off data_offset;
74     Elf_Addr data_vaddr;
75     Elf_Addr data_vlimit;
76     caddr_t data_addr;
77     int data_prot;
78     int data_flags;
79     Elf_Addr clear_vaddr;
80     caddr_t clear_addr;
81     caddr_t clear_page;
82     Elf_Addr phdr_vaddr;
83     size_t nclear, phsize;
84     Elf_Addr bss_vaddr;
85     Elf_Addr bss_vlimit;
86     caddr_t bss_addr;
87     Elf_Word stack_flags;
88     Elf_Addr relro_page;
89     size_t relro_size;
90     Elf_Addr note_start;
91     Elf_Addr note_end;
92     char *note_map;
93     size_t note_map_len;
94
95     hdr = get_elf_header(fd, path, sb);
96     if (hdr == NULL)
97         return (NULL);
98
99     /*
100      * Scan the program header entries, and save key information.
101      *
102      * We expect that the loadable segments are ordered by load address.
103      */
104     phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
105     phsize  = hdr->e_phnum * sizeof (phdr[0]);
106     phlimit = phdr + hdr->e_phnum;
107     nsegs = -1;
108     phdyn = phinterp = phtls = NULL;
109     phdr_vaddr = 0;
110     relro_page = 0;
111     relro_size = 0;
112     note_start = 0;
113     note_end = 0;
114     note_map = NULL;
115     segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
116     stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
117     while (phdr < phlimit) {
118         switch (phdr->p_type) {
119
120         case PT_INTERP:
121             phinterp = phdr;
122             break;
123
124         case PT_LOAD:
125             segs[++nsegs] = phdr;
126             if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
127                 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
128                     path, nsegs);
129                 goto error;
130             }
131             break;
132
133         case PT_PHDR:
134             phdr_vaddr = phdr->p_vaddr;
135             phsize = phdr->p_memsz;
136             break;
137
138         case PT_DYNAMIC:
139             phdyn = phdr;
140             break;
141
142         case PT_TLS:
143             phtls = phdr;
144             break;
145
146         case PT_GNU_STACK:
147             stack_flags = phdr->p_flags;
148             break;
149
150         case PT_GNU_RELRO:
151             relro_page = phdr->p_vaddr;
152             relro_size = phdr->p_memsz;
153             break;
154
155         case PT_NOTE:
156             if (phdr->p_offset > PAGE_SIZE ||
157               phdr->p_offset + phdr->p_filesz > PAGE_SIZE) {
158                 note_map_len = round_page(phdr->p_offset +
159                   phdr->p_filesz) - trunc_page(phdr->p_offset);
160                 note_map = mmap(NULL, note_map_len, PROT_READ,
161                   MAP_PRIVATE, fd, trunc_page(phdr->p_offset));
162                 if (note_map == MAP_FAILED) {
163                     _rtld_error("%s: error mapping PT_NOTE (%d)", path, errno);
164                     goto error;
165                 }
166                 note_start = (Elf_Addr)(note_map + phdr->p_offset -
167                   trunc_page(phdr->p_offset));
168             } else {
169                 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
170             }
171             note_end = note_start + phdr->p_filesz;
172             break;
173         }
174
175         ++phdr;
176     }
177     if (phdyn == NULL) {
178         _rtld_error("%s: object is not dynamically-linked", path);
179         goto error;
180     }
181
182     if (nsegs < 0) {
183         _rtld_error("%s: too few PT_LOAD segments", path);
184         goto error;
185     }
186
187     /*
188      * Map the entire address space of the object, to stake out our
189      * contiguous region, and to establish the base address for relocation.
190      */
191     base_vaddr = trunc_page(segs[0]->p_vaddr);
192     base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
193     mapsize = base_vlimit - base_vaddr;
194     base_addr = (caddr_t) base_vaddr;
195     base_flags = __getosreldate() >= P_OSREL_MAP_GUARD ||
196       (P_OSREL_MAJOR(__getosreldate()) == 11 && __getosreldate() >=
197       P_OSREL_MAP_GUARD_11) ? MAP_GUARD : MAP_PRIVATE | MAP_ANON | MAP_NOCORE;
198     if (npagesizes > 1 && round_page(segs[0]->p_filesz) >= pagesizes[1])
199         base_flags |= MAP_ALIGNED_SUPER;
200     if (base_vaddr != 0)
201         base_flags |= MAP_FIXED | MAP_EXCL;
202
203     mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
204     if (mapbase == (caddr_t) -1) {
205         _rtld_error("%s: mmap of entire address space failed: %s",
206           path, rtld_strerror(errno));
207         goto error;
208     }
209     if (base_addr != NULL && mapbase != base_addr) {
210         _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
211           path, base_addr, mapbase);
212         goto error1;
213     }
214
215     for (i = 0; i <= nsegs; i++) {
216         /* Overlay the segment onto the proper region. */
217         data_offset = trunc_page(segs[i]->p_offset);
218         data_vaddr = trunc_page(segs[i]->p_vaddr);
219         data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
220         data_addr = mapbase + (data_vaddr - base_vaddr);
221         data_prot = convert_prot(segs[i]->p_flags);
222         data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
223         if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
224           data_flags | MAP_PREFAULT_READ, fd, data_offset) == (caddr_t) -1) {
225             _rtld_error("%s: mmap of data failed: %s", path,
226                 rtld_strerror(errno));
227             goto error1;
228         }
229
230         /* Do BSS setup */
231         if (segs[i]->p_filesz != segs[i]->p_memsz) {
232
233             /* Clear any BSS in the last page of the segment. */
234             clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
235             clear_addr = mapbase + (clear_vaddr - base_vaddr);
236             clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
237
238             if ((nclear = data_vlimit - clear_vaddr) > 0) {
239                 /* Make sure the end of the segment is writable */
240                 if ((data_prot & PROT_WRITE) == 0 && -1 ==
241                      mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
242                         _rtld_error("%s: mprotect failed: %s", path,
243                             rtld_strerror(errno));
244                         goto error1;
245                 }
246
247                 memset(clear_addr, 0, nclear);
248
249                 /* Reset the data protection back */
250                 if ((data_prot & PROT_WRITE) == 0)
251                     mprotect(clear_page, PAGE_SIZE, data_prot);
252             }
253
254             /* Overlay the BSS segment onto the proper region. */
255             bss_vaddr = data_vlimit;
256             bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
257             bss_addr = mapbase +  (bss_vaddr - base_vaddr);
258             if (bss_vlimit > bss_vaddr) {       /* There is something to do */
259                 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
260                     data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
261                     _rtld_error("%s: mmap of bss failed: %s", path,
262                         rtld_strerror(errno));
263                     goto error1;
264                 }
265             }
266         }
267
268         if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
269           (data_vlimit - data_vaddr + data_offset) >=
270           (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
271             phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
272         }
273     }
274
275     obj = obj_new();
276     if (sb != NULL) {
277         obj->dev = sb->st_dev;
278         obj->ino = sb->st_ino;
279     }
280     obj->mapbase = mapbase;
281     obj->mapsize = mapsize;
282     obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
283       base_vaddr;
284     obj->vaddrbase = base_vaddr;
285     obj->relocbase = mapbase - base_vaddr;
286     obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
287     if (hdr->e_entry != 0)
288         obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
289     if (phdr_vaddr != 0) {
290         obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr);
291     } else {
292         obj->phdr = malloc(phsize);
293         if (obj->phdr == NULL) {
294             obj_free(obj);
295             _rtld_error("%s: cannot allocate program header", path);
296             goto error1;
297         }
298         memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize);
299         obj->phdr_alloc = true;
300     }
301     obj->phsize = phsize;
302     if (phinterp != NULL)
303         obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
304     if (phtls != NULL) {
305         tls_dtv_generation++;
306         obj->tlsindex = ++tls_max_index;
307         obj->tlssize = phtls->p_memsz;
308         obj->tlsalign = phtls->p_align;
309         obj->tlsinitsize = phtls->p_filesz;
310         obj->tlsinit = mapbase + phtls->p_vaddr;
311     }
312     obj->stack_flags = stack_flags;
313     obj->relro_page = obj->relocbase + trunc_page(relro_page);
314     obj->relro_size = round_page(relro_size);
315     if (note_start < note_end)
316         digest_notes(obj, note_start, note_end);
317     if (note_map != NULL)
318         munmap(note_map, note_map_len);
319     munmap(hdr, PAGE_SIZE);
320     return (obj);
321
322 error1:
323     munmap(mapbase, mapsize);
324 error:
325     if (note_map != NULL && note_map != MAP_FAILED)
326         munmap(note_map, note_map_len);
327     munmap(hdr, PAGE_SIZE);
328     return (NULL);
329 }
330
331 static Elf_Ehdr *
332 get_elf_header(int fd, const char *path, const struct stat *sbp)
333 {
334         Elf_Ehdr *hdr;
335
336         /* Make sure file has enough data for the ELF header */
337         if (sbp != NULL && sbp->st_size < sizeof(Elf_Ehdr)) {
338                 _rtld_error("%s: invalid file format", path);
339                 return (NULL);
340         }
341
342         hdr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
343             fd, 0);
344         if (hdr == (Elf_Ehdr *)MAP_FAILED) {
345                 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
346                 return (NULL);
347         }
348
349         /* Make sure the file is valid */
350         if (!IS_ELF(*hdr)) {
351                 _rtld_error("%s: invalid file format", path);
352                 goto error;
353         }
354         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
355             hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
356                 _rtld_error("%s: unsupported file layout", path);
357                 goto error;
358         }
359         if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
360             hdr->e_version != EV_CURRENT) {
361                 _rtld_error("%s: unsupported file version", path);
362                 goto error;
363         }
364         if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
365                 _rtld_error("%s: unsupported file type", path);
366                 goto error;
367         }
368         if (hdr->e_machine != ELF_TARG_MACH) {
369                 _rtld_error("%s: unsupported machine", path);
370                 goto error;
371         }
372
373         /*
374          * We rely on the program header being in the first page.  This is
375          * not strictly required by the ABI specification, but it seems to
376          * always true in practice.  And, it simplifies things considerably.
377          */
378         if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
379                 _rtld_error(
380             "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
381                 goto error;
382         }
383         if (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) >
384             (size_t)PAGE_SIZE) {
385                 _rtld_error("%s: program header too large", path);
386                 goto error;
387         }
388         return (hdr);
389
390 error:
391         munmap(hdr, PAGE_SIZE);
392         return (NULL);
393 }
394
395 void
396 obj_free(Obj_Entry *obj)
397 {
398     Objlist_Entry *elm;
399
400     if (obj->tls_done)
401         free_tls_offset(obj);
402     while (obj->needed != NULL) {
403         Needed_Entry *needed = obj->needed;
404         obj->needed = needed->next;
405         free(needed);
406     }
407     while (!STAILQ_EMPTY(&obj->names)) {
408         Name_Entry *entry = STAILQ_FIRST(&obj->names);
409         STAILQ_REMOVE_HEAD(&obj->names, link);
410         free(entry);
411     }
412     while (!STAILQ_EMPTY(&obj->dldags)) {
413         elm = STAILQ_FIRST(&obj->dldags);
414         STAILQ_REMOVE_HEAD(&obj->dldags, link);
415         free(elm);
416     }
417     while (!STAILQ_EMPTY(&obj->dagmembers)) {
418         elm = STAILQ_FIRST(&obj->dagmembers);
419         STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
420         free(elm);
421     }
422     if (obj->vertab)
423         free(obj->vertab);
424     if (obj->origin_path)
425         free(obj->origin_path);
426     if (obj->z_origin)
427         free(obj->rpath);
428     if (obj->priv)
429         free(obj->priv);
430     if (obj->path)
431         free(obj->path);
432     if (obj->phdr_alloc)
433         free((void *)obj->phdr);
434     free(obj);
435 }
436
437 Obj_Entry *
438 obj_new(void)
439 {
440     Obj_Entry *obj;
441
442     obj = CNEW(Obj_Entry);
443     STAILQ_INIT(&obj->dldags);
444     STAILQ_INIT(&obj->dagmembers);
445     STAILQ_INIT(&obj->names);
446     return obj;
447 }
448
449 /*
450  * Given a set of ELF protection flags, return the corresponding protection
451  * flags for MMAP.
452  */
453 int
454 convert_prot(int elfflags)
455 {
456     int prot = 0;
457     if (elfflags & PF_R)
458         prot |= PROT_READ;
459     if (elfflags & PF_W)
460         prot |= PROT_WRITE;
461     if (elfflags & PF_X)
462         prot |= PROT_EXEC;
463     return prot;
464 }
465
466 static int
467 convert_flags(int elfflags)
468 {
469     int flags = MAP_PRIVATE; /* All mappings are private */
470
471     /*
472      * Readonly mappings are marked "MAP_NOCORE", because they can be
473      * reconstructed by a debugger.
474      */
475     if (!(elfflags & PF_W))
476         flags |= MAP_NOCORE;
477     return flags;
478 }