map_object.c revision 1.44 1 /* $NetBSD: map_object.c,v 1.44 2012/07/25 22:51:04 martin 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.44 2012/07/25 22:51:04 martin 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 "debug.h"
50 #include "rtld.h"
51
52 static int protflags(int); /* Elf flags -> mmap protection */
53
54 #define EA_UNDEF (~(Elf_Addr)0)
55
56 /*
57 * Map a shared object into memory. The argument is a file descriptor,
58 * which must be open on the object and positioned at its beginning.
59 *
60 * The return value is a pointer to a newly-allocated Obj_Entry structure
61 * for the shared object. Returns NULL on failure.
62 */
63 Obj_Entry *
64 _rtld_map_object(const char *path, int fd, const struct stat *sb)
65 {
66 Obj_Entry *obj;
67 Elf_Ehdr *ehdr;
68 Elf_Phdr *phdr;
69 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
70 Elf_Phdr *phtls;
71 #endif
72 size_t phsize;
73 Elf_Phdr *phlimit;
74 Elf_Phdr *segs[2];
75 int nsegs;
76 caddr_t mapbase = MAP_FAILED;
77 size_t mapsize = 0;
78 int mapflags;
79 Elf_Off base_offset;
80 #ifdef MAP_ALIGNED
81 Elf_Addr base_alignment;
82 #endif
83 Elf_Addr base_vaddr;
84 Elf_Addr base_vlimit;
85 Elf_Addr text_vlimit;
86 int text_flags;
87 caddr_t base_addr;
88 Elf_Off data_offset;
89 Elf_Addr data_vaddr;
90 Elf_Addr data_vlimit;
91 int data_flags;
92 caddr_t data_addr;
93 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
94 Elf_Addr tls_vaddr = 0; /* Noise GCC */
95 #endif
96 Elf_Addr phdr_vaddr;
97 size_t phdr_memsz;
98 caddr_t gap_addr;
99 size_t gap_size;
100 int i;
101 #ifdef RTLD_LOADER
102 Elf_Addr clear_vaddr;
103 caddr_t clear_addr;
104 size_t nclear;
105 #endif
106
107 if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) {
108 _rtld_error("%s: unrecognized file format1", path);
109 return NULL;
110 }
111
112 obj = _rtld_obj_new();
113 obj->path = xstrdup(path);
114 obj->pathlen = strlen(path);
115 if (sb != NULL) {
116 obj->dev = sb->st_dev;
117 obj->ino = sb->st_ino;
118 }
119
120 ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
121 (off_t)0);
122 obj->ehdr = ehdr;
123 if (ehdr == MAP_FAILED) {
124 _rtld_error("%s: read error: %s", path, xstrerror(errno));
125 goto bad;
126 }
127 /* Make sure the file is valid */
128 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
129 ehdr->e_ident[EI_CLASS] != ELFCLASS) {
130 _rtld_error("%s: unrecognized file format2 [%x != %x]", path,
131 ehdr->e_ident[EI_CLASS], ELFCLASS);
132 goto bad;
133 }
134 /* Elf_e_ident includes class */
135 if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
136 ehdr->e_version != EV_CURRENT ||
137 ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
138 _rtld_error("%s: unsupported file version", path);
139 goto bad;
140 }
141 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
142 _rtld_error("%s: unsupported file type", path);
143 goto bad;
144 }
145 switch (ehdr->e_machine) {
146 ELFDEFNNAME(MACHDEP_ID_CASES)
147 default:
148 _rtld_error("%s: unsupported machine", path);
149 goto bad;
150 }
151
152 /*
153 * We rely on the program header being in the first page. This is
154 * not strictly required by the ABI specification, but it seems to
155 * always true in practice. And, it simplifies things considerably.
156 */
157 assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
158 assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
159 _rtld_pagesz);
160
161 /*
162 * Scan the program header entries, and save key information.
163 *
164 * We rely on there being exactly two load segments, text and data,
165 * in that order.
166 */
167 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
168 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
169 phtls = NULL;
170 #endif
171 phsize = ehdr->e_phnum * sizeof(phdr[0]);
172 obj->phdr = NULL;
173 phdr_vaddr = EA_UNDEF;
174 phdr_memsz = 0;
175 phlimit = phdr + ehdr->e_phnum;
176 nsegs = 0;
177 while (phdr < phlimit) {
178 switch (phdr->p_type) {
179 case PT_INTERP:
180 obj->interp = (void *)(uintptr_t)phdr->p_vaddr;
181 dbg(("%s: PT_INTERP %p", obj->path, obj->interp));
182 break;
183
184 case PT_LOAD:
185 if (nsegs < 2)
186 segs[nsegs] = phdr;
187 ++nsegs;
188
189 #if ELFSIZE == 64
190 #define PRImemsz PRIu64
191 #else
192 #define PRImemsz PRIu32
193 #endif
194 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_LOAD",
195 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
196 break;
197
198 case PT_PHDR:
199 phdr_vaddr = phdr->p_vaddr;
200 phdr_memsz = phdr->p_memsz;
201 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_PHDR",
202 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
203 break;
204
205 case PT_DYNAMIC:
206 obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr;
207 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_DYNAMIC",
208 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
209 break;
210
211 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
212 case PT_TLS:
213 phtls = phdr;
214 dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_TLS",
215 (void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
216 break;
217 #endif
218 }
219
220 ++phdr;
221 }
222 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
223 obj->entry = (void *)(uintptr_t)ehdr->e_entry;
224 if (!obj->dynamic) {
225 _rtld_error("%s: not dynamically linked", path);
226 goto bad;
227 }
228 if (nsegs != 2) {
229 _rtld_error("%s: wrong number of segments (%d != 2)", path,
230 nsegs);
231 goto bad;
232 }
233
234 /*
235 * Map the entire address space of the object as a file
236 * region to stake out our contiguous region and establish a
237 * base for relocation. We use a file mapping so that
238 * the kernel will give us whatever alignment is appropriate
239 * for the platform we're running on.
240 *
241 * We map it using the text protection, map the data segment
242 * into the right place, then map an anon segment for the bss
243 * and unmap the gaps left by padding to alignment.
244 */
245
246 #ifdef MAP_ALIGNED
247 base_alignment = segs[0]->p_align;
248 #endif
249 base_offset = round_down(segs[0]->p_offset);
250 base_vaddr = round_down(segs[0]->p_vaddr);
251 base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
252 text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
253 text_flags = protflags(segs[0]->p_flags);
254 data_offset = round_down(segs[1]->p_offset);
255 data_vaddr = round_down(segs[1]->p_vaddr);
256 data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
257 data_flags = protflags(segs[1]->p_flags);
258 #ifdef RTLD_LOADER
259 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
260 #endif
261
262 obj->textsize = text_vlimit - base_vaddr;
263 obj->vaddrbase = base_vaddr;
264 obj->isdynamic = ehdr->e_type == ET_DYN;
265
266 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
267 if (phtls != NULL) {
268 ++_rtld_tls_dtv_generation;
269 obj->tlsindex = ++_rtld_tls_max_index;
270 obj->tlssize = phtls->p_memsz;
271 obj->tlsalign = phtls->p_align;
272 obj->tlsinitsize = phtls->p_filesz;
273 tls_vaddr = phtls->p_vaddr;
274 }
275 #endif
276
277 obj->phdr_loaded = false;
278 for (i = 0; i < nsegs; i++) {
279 if (phdr_vaddr != EA_UNDEF &&
280 segs[i]->p_vaddr <= phdr_vaddr &&
281 segs[i]->p_memsz >= phdr_memsz) {
282 obj->phdr_loaded = true;
283 break;
284 }
285 if (segs[i]->p_offset <= ehdr->e_phoff &&
286 segs[i]->p_memsz >= phsize) {
287 phdr_vaddr = segs[i]->p_vaddr + ehdr->e_phoff;
288 phdr_memsz = phsize;
289 obj->phdr_loaded = true;
290 break;
291 }
292 }
293 if (obj->phdr_loaded) {
294 obj->phdr = (void *)(uintptr_t)phdr_vaddr;
295 obj->phsize = phdr_memsz;
296 } else {
297 Elf_Phdr *buf;
298 buf = xmalloc(phsize);
299 if (buf == NULL) {
300 _rtld_error("%s: cannot allocate program header", path);
301 goto bad;
302 }
303 memcpy(buf, phdr, phsize);
304 obj->phdr = buf;
305 obj->phsize = phsize;
306 }
307 dbg(("%s: phdr %p phsize %zu (%s)", obj->path, obj->phdr, obj->phsize,
308 obj->phdr_loaded ? "loaded" : "allocated"));
309
310 /* Unmap header if it overlaps the first load section. */
311 if (base_offset < _rtld_pagesz) {
312 munmap(ehdr, _rtld_pagesz);
313 obj->ehdr = MAP_FAILED;
314 }
315
316 /*
317 * Calculate log2 of the base section alignment.
318 */
319 mapflags = 0;
320 #ifdef MAP_ALIGNED
321 if (base_alignment > _rtld_pagesz) {
322 unsigned int log2 = 0;
323 for (; base_alignment > 1; base_alignment >>= 1)
324 log2++;
325 mapflags = MAP_ALIGNED(log2);
326 }
327 #endif
328
329 #ifdef RTLD_LOADER
330 base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr;
331 #else
332 base_addr = NULL;
333 #endif
334 mapsize = base_vlimit - base_vaddr;
335 mapbase = mmap(base_addr, mapsize, text_flags,
336 mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset);
337 if (mapbase == MAP_FAILED) {
338 _rtld_error("mmap of entire address space failed: %s",
339 xstrerror(errno));
340 goto bad;
341 }
342
343 /* Overlay the data segment onto the proper region. */
344 data_addr = mapbase + (data_vaddr - base_vaddr);
345 if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
346 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
347 MAP_FAILED) {
348 _rtld_error("mmap of data failed: %s", xstrerror(errno));
349 goto bad;
350 }
351
352 /* Overlay the bss segment onto the proper region. */
353 if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
354 data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) ==
355 MAP_FAILED) {
356 _rtld_error("mmap of bss failed: %s", xstrerror(errno));
357 goto bad;
358 }
359
360 /* Unmap the gap between the text and data. */
361 gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
362 gap_size = data_addr - gap_addr;
363 if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
364 _rtld_error("mprotect of text -> data gap failed: %s",
365 xstrerror(errno));
366 goto bad;
367 }
368
369 #ifdef RTLD_LOADER
370 /* Clear any BSS in the last page of the data segment. */
371 clear_addr = mapbase + (clear_vaddr - base_vaddr);
372 if ((nclear = data_vlimit - clear_vaddr) > 0)
373 memset(clear_addr, 0, nclear);
374
375 /* Non-file portion of BSS mapped above. */
376 #endif
377
378 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
379 if (phtls != NULL)
380 obj->tlsinit = mapbase + tls_vaddr;
381 #endif
382
383 obj->mapbase = mapbase;
384 obj->mapsize = mapsize;
385 obj->relocbase = mapbase - base_vaddr;
386
387 if (obj->dynamic)
388 obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic);
389 if (obj->entry)
390 obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry);
391 if (obj->interp)
392 obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp);
393 if (obj->phdr_loaded)
394 obj->phdr = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->phdr);
395
396 return obj;
397
398 bad:
399 if (obj->ehdr != MAP_FAILED)
400 munmap(obj->ehdr, _rtld_pagesz);
401 if (mapbase != MAP_FAILED)
402 munmap(mapbase, mapsize);
403 _rtld_obj_free(obj);
404 return NULL;
405 }
406
407 void
408 _rtld_obj_free(Obj_Entry *obj)
409 {
410 Objlist_Entry *elm;
411
412 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
413 if (obj->tls_done)
414 _rtld_tls_offset_free(obj);
415 #endif
416 xfree(obj->path);
417 while (obj->needed != NULL) {
418 Needed_Entry *needed = obj->needed;
419 obj->needed = needed->next;
420 xfree(needed);
421 }
422 while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
423 SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
424 xfree(elm);
425 }
426 while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
427 SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
428 xfree(elm);
429 }
430 if (!obj->phdr_loaded)
431 xfree((void *)(uintptr_t)obj->phdr);
432 xfree(obj);
433 #ifdef COMBRELOC
434 _rtld_combreloc_reset(obj);
435 #endif
436 }
437
438 Obj_Entry *
439 _rtld_obj_new(void)
440 {
441 Obj_Entry *obj;
442
443 obj = CNEW(Obj_Entry);
444 SIMPLEQ_INIT(&obj->dldags);
445 SIMPLEQ_INIT(&obj->dagmembers);
446 return obj;
447 }
448
449 /*
450 * Given a set of ELF protection flags, return the corresponding protection
451 * flags for MMAP.
452 */
453 static int
454 protflags(int elfflags)
455 {
456 int prot = 0;
457
458 if (elfflags & PF_R)
459 prot |= PROT_READ;
460 #ifdef RTLD_LOADER
461 if (elfflags & PF_W)
462 prot |= PROT_WRITE;
463 #endif
464 if (elfflags & PF_X)
465 prot |= PROT_EXEC;
466 return prot;
467 }
468