xref: /freebsd/libexec/rtld-elf/map_object.c (revision 7f3dea244c40159a41ab22da77a434d7c5b5e85a)
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 
31 #include <errno.h>
32 #include <stddef.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "rtld.h"
37 
38 static int protflags(int);	/* Elf flags -> mmap protection */
39 
40 /*
41  * Map a shared object into memory.  The "fd" argument is a file descriptor,
42  * which must be open on the object and positioned at its beginning.
43  * The "path" argument is a pathname that is used only for error messages.
44  *
45  * The return value is a pointer to a newly-allocated Obj_Entry structure
46  * for the shared object.  Returns NULL on failure.
47  */
48 Obj_Entry *
49 map_object(int fd, const char *path)
50 {
51     Obj_Entry *obj;
52     union {
53 	Elf_Ehdr hdr;
54 	char buf[PAGE_SIZE];
55     } u;
56     int nbytes;
57     Elf_Phdr *phdr;
58     Elf_Phdr *phlimit;
59     Elf_Phdr *segs[2];
60     int nsegs;
61     Elf_Phdr *phdyn;
62     Elf_Phdr *phphdr;
63     caddr_t mapbase;
64     size_t mapsize;
65     Elf_Off base_offset;
66     Elf_Addr base_vaddr;
67     Elf_Addr base_vlimit;
68     caddr_t base_addr;
69     Elf_Off data_offset;
70     Elf_Addr data_vaddr;
71     Elf_Addr data_vlimit;
72     caddr_t data_addr;
73     Elf_Addr clear_vaddr;
74     caddr_t clear_addr;
75     size_t nclear;
76     Elf_Addr bss_vaddr;
77     Elf_Addr bss_vlimit;
78     caddr_t bss_addr;
79 
80     if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
81 	_rtld_error("%s: read error: %s", path, strerror(errno));
82 	return NULL;
83     }
84 
85     /* Make sure the file is valid */
86     if (nbytes < sizeof(Elf_Ehdr)
87       || u.hdr.e_ident[EI_MAG0] != ELFMAG0
88       || u.hdr.e_ident[EI_MAG1] != ELFMAG1
89       || u.hdr.e_ident[EI_MAG2] != ELFMAG2
90       || u.hdr.e_ident[EI_MAG3] != ELFMAG3) {
91 	_rtld_error("%s: invalid file format", path);
92 	return NULL;
93     }
94     if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
95       || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
96 	_rtld_error("%s: unsupported file layout", path);
97 	return NULL;
98     }
99     if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
100       || u.hdr.e_version != EV_CURRENT) {
101 	_rtld_error("%s: unsupported file version", path);
102 	return NULL;
103     }
104     if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
105 	_rtld_error("%s: unsupported file type", path);
106 	return NULL;
107     }
108     if (u.hdr.e_machine != ELF_TARG_MACH) {
109 	_rtld_error("%s: unsupported machine", path);
110 	return NULL;
111     }
112 
113     /*
114      * We rely on the program header being in the first page.  This is
115      * not strictly required by the ABI specification, but it seems to
116      * always true in practice.  And, it simplifies things considerably.
117      */
118     if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
119 	_rtld_error(
120 	  "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
121 	return NULL;
122     }
123     if (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) > nbytes) {
124 	_rtld_error("%s: program header too large", path);
125 	return NULL;
126     }
127 
128     /*
129      * Scan the program header entries, and save key information.
130      *
131      * We rely on there being exactly two load segments, text and data,
132      * in that order.
133      */
134     phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff);
135     phlimit = phdr + u.hdr.e_phnum;
136     nsegs = 0;
137     phdyn = NULL;
138     phphdr = NULL;
139     while (phdr < phlimit) {
140 	switch (phdr->p_type) {
141 
142 	case PT_LOAD:
143 	    if (nsegs >= 2) {
144 		_rtld_error("%s: too many PT_LOAD segments", path);
145 		return NULL;
146 	    }
147 	    segs[nsegs] = phdr;
148 	    ++nsegs;
149 	    break;
150 
151 	case PT_PHDR:
152 	    phphdr = phdr;
153 	    break;
154 
155 	case PT_DYNAMIC:
156 	    phdyn = phdr;
157 	    break;
158 	}
159 
160 	++phdr;
161     }
162     if (phdyn == NULL) {
163 	_rtld_error("%s: object is not dynamically-linked", path);
164 	return NULL;
165     }
166 
167     if (nsegs < 2) {
168 	_rtld_error("%s: too few PT_LOAD segments", path);
169 	return NULL;
170     }
171     if (segs[0]->p_align < PAGE_SIZE || segs[1]->p_align < PAGE_SIZE) {
172 	_rtld_error("%s: PT_LOAD segments not page-aligned", path);
173 	return NULL;
174     }
175 
176     /*
177      * Map the entire address space of the object, to stake out our
178      * contiguous region, and to establish the base address for relocation.
179      */
180     base_offset = trunc_page(segs[0]->p_offset);
181     base_vaddr = trunc_page(segs[0]->p_vaddr);
182     base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
183     mapsize = base_vlimit - base_vaddr;
184     base_addr = u.hdr.e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
185 
186     mapbase = mmap(base_addr, mapsize, protflags(segs[0]->p_flags),
187       MAP_PRIVATE, fd, base_offset);
188     if (mapbase == (caddr_t) -1) {
189 	_rtld_error("%s: mmap of entire address space failed: %s",
190 	  path, strerror(errno));
191 	return NULL;
192     }
193     if (base_addr != NULL && mapbase != base_addr) {
194 	_rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
195 	  path, base_addr, mapbase);
196 	munmap(mapbase, mapsize);
197 	return NULL;
198     }
199 
200     /* Overlay the data segment onto the proper region. */
201     data_offset = trunc_page(segs[1]->p_offset);
202     data_vaddr = trunc_page(segs[1]->p_vaddr);
203     data_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_filesz);
204     data_addr = mapbase + (data_vaddr - base_vaddr);
205     if (mmap(data_addr, data_vlimit - data_vaddr, protflags(segs[1]->p_flags),
206       MAP_PRIVATE|MAP_FIXED, fd, data_offset) == (caddr_t) -1) {
207 	_rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
208 	return NULL;
209     }
210 
211     /* Clear any BSS in the last page of the data segment. */
212     clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
213     clear_addr = mapbase + (clear_vaddr - base_vaddr);
214     if ((nclear = data_vlimit - clear_vaddr) > 0)
215 	memset(clear_addr, 0, nclear);
216 
217     /* Overlay the BSS segment onto the proper region. */
218     bss_vaddr = data_vlimit;
219     bss_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
220     bss_addr = mapbase +  (bss_vaddr - base_vaddr);
221     if (bss_vlimit > bss_vaddr) {	/* There is something to do */
222 	if (mmap(bss_addr, bss_vlimit - bss_vaddr, protflags(segs[1]->p_flags),
223 	  MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
224 	    _rtld_error("%s: mmap of bss failed: %s", path, strerror(errno));
225 	    return NULL;
226 	}
227     }
228 
229     obj = CNEW(Obj_Entry);
230     obj->mapbase = mapbase;
231     obj->mapsize = mapsize;
232     obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
233       base_vaddr;
234     obj->vaddrbase = base_vaddr;
235     obj->relocbase = mapbase - base_vaddr;
236     obj->dynamic = (const Elf_Dyn *)
237       (mapbase + (phdyn->p_vaddr - base_vaddr));
238     if (u.hdr.e_entry != 0)
239 	obj->entry = (caddr_t) (mapbase + (u.hdr.e_entry - base_vaddr));
240     if (phphdr != NULL) {
241 	obj->phdr = (const Elf_Phdr *)
242 	  (mapbase + (phphdr->p_vaddr - base_vaddr));
243 	obj->phsize = phphdr->p_memsz;
244     }
245 
246     return obj;
247 }
248 
249 /*
250  * Given a set of ELF protection flags, return the corresponding protection
251  * flags for MMAP.
252  */
253 static int
254 protflags(int elfflags)
255 {
256     int prot = 0;
257     if (elfflags & PF_R)
258 	prot |= PROT_READ;
259     if (elfflags & PF_W)
260 	prot |= PROT_WRITE;
261     if (elfflags & PF_X)
262 	prot |= PROT_EXEC;
263     return prot;
264 }
265