map_object.c revision 1.40 1 /* $NetBSD: map_object.c,v 1.40 2010/09/11 11:11:52 skrll Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root (at) ihack.net>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by John Polstra.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __RCSID("$NetBSD: map_object.c,v 1.40 2010/09/11 11:11:52 skrll Exp $");
38 #endif /* not lint */
39
40 #include <errno.h>
41 #include <stddef.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <sys/mman.h>
48
49 #include "rtld.h"
50
51 static int protflags(int); /* Elf flags -> mmap protection */
52
53 /*
54 * Map a shared object into memory. The argument is a file descriptor,
55 * which must be open on the object and positioned at its beginning.
56 *
57 * The return value is a pointer to a newly-allocated Obj_Entry structure
58 * for the shared object. Returns NULL on failure.
59 */
60 Obj_Entry *
61 _rtld_map_object(const char *path, int fd, const struct stat *sb)
62 {
63 Obj_Entry *obj;
64 Elf_Ehdr *ehdr;
65 Elf_Phdr *phdr;
66 Elf_Phdr *phlimit;
67 Elf_Phdr *segs[2];
68 int nsegs;
69 caddr_t mapbase = MAP_FAILED;
70 size_t mapsize = 0;
71 int mapflags;
72 Elf_Off base_offset;
73 #ifdef MAP_ALIGNED
74 Elf_Addr base_alignment;
75 #endif
76 Elf_Addr base_vaddr;
77 Elf_Addr base_vlimit;
78 Elf_Addr text_vlimit;
79 int text_flags;
80 caddr_t base_addr;
81 Elf_Off data_offset;
82 Elf_Addr data_vaddr;
83 Elf_Addr data_vlimit;
84 int data_flags;
85 caddr_t data_addr;
86 caddr_t gap_addr;
87 size_t gap_size;
88 #ifdef RTLD_LOADER
89 Elf_Addr clear_vaddr;
90 caddr_t clear_addr;
91 size_t nclear;
92 #endif
93
94 if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) {
95 _rtld_error("%s: unrecognized file format1", path);
96 return NULL;
97 }
98
99 obj = _rtld_obj_new();
100 obj->path = xstrdup(path);
101 obj->pathlen = strlen(path);
102 if (sb != NULL) {
103 obj->dev = sb->st_dev;
104 obj->ino = sb->st_ino;
105 }
106
107 ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
108 (off_t)0);
109 obj->ehdr = ehdr;
110 if (ehdr == MAP_FAILED) {
111 _rtld_error("%s: read error: %s", path, xstrerror(errno));
112 goto bad;
113 }
114 /* Make sure the file is valid */
115 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
116 ehdr->e_ident[EI_CLASS] != ELFCLASS) {
117 _rtld_error("%s: unrecognized file format2 [%x != %x]", path,
118 ehdr->e_ident[EI_CLASS], ELFCLASS);
119 goto bad;
120 }
121 /* Elf_e_ident includes class */
122 if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
123 ehdr->e_version != EV_CURRENT ||
124 ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
125 _rtld_error("%s: unsupported file version", path);
126 goto bad;
127 }
128 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
129 _rtld_error("%s: unsupported file type", path);
130 goto bad;
131 }
132 switch (ehdr->e_machine) {
133 ELFDEFNNAME(MACHDEP_ID_CASES)
134 default:
135 _rtld_error("%s: unsupported machine", path);
136 goto bad;
137 }
138
139 /*
140 * We rely on the program header being in the first page. This is
141 * not strictly required by the ABI specification, but it seems to
142 * always true in practice. And, it simplifies things considerably.
143 */
144 assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
145 assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
146 _rtld_pagesz);
147
148 /*
149 * Scan the program header entries, and save key information.
150 *
151 * We rely on there being exactly two load segments, text and data,
152 * in that order.
153 */
154 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
155 phlimit = phdr + ehdr->e_phnum;
156 nsegs = 0;
157 while (phdr < phlimit) {
158 switch (phdr->p_type) {
159 case PT_INTERP:
160 obj->interp = (void *)(uintptr_t)phdr->p_vaddr;
161 break;
162
163 case PT_LOAD:
164 if (nsegs < 2)
165 segs[nsegs] = phdr;
166 ++nsegs;
167 break;
168
169 case PT_DYNAMIC:
170 obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr;
171 break;
172 }
173
174 ++phdr;
175 }
176 obj->entry = (void *)(uintptr_t)ehdr->e_entry;
177 if (!obj->dynamic) {
178 _rtld_error("%s: not dynamically linked", path);
179 goto bad;
180 }
181 if (nsegs != 2) {
182 _rtld_error("%s: wrong number of segments (%d != 2)", path,
183 nsegs);
184 goto bad;
185 }
186
187 /*
188 * Map the entire address space of the object as a file
189 * region to stake out our contiguous region and establish a
190 * base for relocation. We use a file mapping so that
191 * the kernel will give us whatever alignment is appropriate
192 * for the platform we're running on.
193 *
194 * We map it using the text protection, map the data segment
195 * into the right place, then map an anon segment for the bss
196 * and unmap the gaps left by padding to alignment.
197 */
198
199 #ifdef MAP_ALIGNED
200 base_alignment = segs[0]->p_align;
201 #endif
202 base_offset = round_down(segs[0]->p_offset);
203 base_vaddr = round_down(segs[0]->p_vaddr);
204 base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
205 text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
206 text_flags = protflags(segs[0]->p_flags);
207 data_offset = round_down(segs[1]->p_offset);
208 data_vaddr = round_down(segs[1]->p_vaddr);
209 data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
210 data_flags = protflags(segs[1]->p_flags);
211 #ifdef RTLD_LOADER
212 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
213 #endif
214
215 obj->textsize = text_vlimit - base_vaddr;
216 obj->vaddrbase = base_vaddr;
217 obj->isdynamic = ehdr->e_type == ET_DYN;
218
219 /* Unmap header if it overlaps the first load section. */
220 if (base_offset < _rtld_pagesz) {
221 munmap(ehdr, _rtld_pagesz);
222 obj->ehdr = MAP_FAILED;
223 }
224
225 /*
226 * Calculate log2 of the base section alignment.
227 */
228 mapflags = 0;
229 #ifdef MAP_ALIGNED
230 if (base_alignment > _rtld_pagesz) {
231 unsigned int log2 = 0;
232 for (; base_alignment > 1; base_alignment >>= 1)
233 log2++;
234 mapflags = MAP_ALIGNED(log2);
235 }
236 #endif
237
238 #ifdef RTLD_LOADER
239 base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr;
240 #else
241 base_addr = NULL;
242 #endif
243 mapsize = base_vlimit - base_vaddr;
244 mapbase = mmap(base_addr, mapsize, text_flags,
245 mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset);
246 if (mapbase == MAP_FAILED) {
247 _rtld_error("mmap of entire address space failed: %s",
248 xstrerror(errno));
249 goto bad;
250 }
251
252 /* Overlay the data segment onto the proper region. */
253 data_addr = mapbase + (data_vaddr - base_vaddr);
254 if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
255 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
256 MAP_FAILED) {
257 _rtld_error("mmap of data failed: %s", xstrerror(errno));
258 goto bad;
259 }
260
261 /* Overlay the bss segment onto the proper region. */
262 if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
263 data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) ==
264 MAP_FAILED) {
265 _rtld_error("mmap of bss failed: %s", xstrerror(errno));
266 goto bad;
267 }
268
269 /* Unmap the gap between the text and data. */
270 gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
271 gap_size = data_addr - gap_addr;
272 if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
273 _rtld_error("mprotect of text -> data gap failed: %s",
274 xstrerror(errno));
275 goto bad;
276 }
277
278 #ifdef RTLD_LOADER
279 /* Clear any BSS in the last page of the data segment. */
280 clear_addr = mapbase + (clear_vaddr - base_vaddr);
281 if ((nclear = data_vlimit - clear_vaddr) > 0)
282 memset(clear_addr, 0, nclear);
283
284 /* Non-file portion of BSS mapped above. */
285 #endif
286
287 obj->mapbase = mapbase;
288 obj->mapsize = mapsize;
289 obj->relocbase = mapbase - base_vaddr;
290
291 if (obj->dynamic)
292 obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic);
293 if (obj->entry)
294 obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry);
295 if (obj->interp)
296 obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp);
297
298 return obj;
299
300 bad:
301 if (obj->ehdr != MAP_FAILED)
302 munmap(obj->ehdr, _rtld_pagesz);
303 if (mapbase != MAP_FAILED)
304 munmap(mapbase, mapsize);
305 _rtld_obj_free(obj);
306 return NULL;
307 }
308
309 void
310 _rtld_obj_free(Obj_Entry *obj)
311 {
312 Objlist_Entry *elm;
313
314 xfree(obj->path);
315 while (obj->needed != NULL) {
316 Needed_Entry *needed = obj->needed;
317 obj->needed = needed->next;
318 xfree(needed);
319 }
320 while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
321 SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
322 xfree(elm);
323 }
324 while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
325 SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
326 xfree(elm);
327 }
328 xfree(obj);
329 #ifdef COMBRELOC
330 _rtld_combreloc_reset(obj);
331 #endif
332 }
333
334 Obj_Entry *
335 _rtld_obj_new(void)
336 {
337 Obj_Entry *obj;
338
339 obj = CNEW(Obj_Entry);
340 SIMPLEQ_INIT(&obj->dldags);
341 SIMPLEQ_INIT(&obj->dagmembers);
342 return obj;
343 }
344
345 /*
346 * Given a set of ELF protection flags, return the corresponding protection
347 * flags for MMAP.
348 */
349 static int
350 protflags(int elfflags)
351 {
352 int prot = 0;
353
354 if (elfflags & PF_R)
355 prot |= PROT_READ;
356 #ifdef RTLD_LOADER
357 if (elfflags & PF_W)
358 prot |= PROT_WRITE;
359 #endif
360 if (elfflags & PF_X)
361 prot |= PROT_EXEC;
362 return prot;
363 }
364