Bring in various fixes from FreeBSD:
[dragonfly.git] / libexec / rtld-elf / map_object.c
CommitLineData
984263bc
MD
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: src/libexec/rtld-elf/map_object.c,v 1.7.2.2 2002/12/28 19:49:41 dillon Exp $
1c76efe5 26 * $DragonFly: src/libexec/rtld-elf/map_object.c,v 1.3 2003/09/18 21:22:56 dillon Exp $
984263bc
MD
27 */
28
29#include <sys/param.h>
30#include <sys/mman.h>
31#include <sys/stat.h>
32
33#include <errno.h>
34#include <stddef.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39#include "rtld.h"
40
1c76efe5 41static Elf_Ehdr *get_elf_header (int, const char *);
984263bc
MD
42static int convert_prot(int); /* Elf flags -> mmap protection */
43static int convert_flags(int); /* Elf flags -> mmap flags */
44
45/*
46 * Map a shared object into memory. The "fd" argument is a file descriptor,
47 * which must be open on the object and positioned at its beginning.
48 * The "path" argument is a pathname that is used only for error messages.
49 *
50 * The return value is a pointer to a newly-allocated Obj_Entry structure
51 * for the shared object. Returns NULL on failure.
52 */
53Obj_Entry *
54map_object(int fd, const char *path, const struct stat *sb)
55{
56 Obj_Entry *obj;
1c76efe5
MD
57 Elf_Ehdr *hdr;
58 int i;
984263bc
MD
59 Elf_Phdr *phdr;
60 Elf_Phdr *phlimit;
61 Elf_Phdr **segs;
62 int nsegs;
63 Elf_Phdr *phdyn;
64 Elf_Phdr *phphdr;
65 Elf_Phdr *phinterp;
66 caddr_t mapbase;
67 size_t mapsize;
68 Elf_Off base_offset;
69 Elf_Addr base_vaddr;
70 Elf_Addr base_vlimit;
71 caddr_t base_addr;
72 Elf_Off data_offset;
73 Elf_Addr data_vaddr;
74 Elf_Addr data_vlimit;
75 caddr_t data_addr;
76 int data_prot;
77 int data_flags;
78 Elf_Addr clear_vaddr;
79 caddr_t clear_addr;
80 caddr_t clear_page;
81 size_t nclear;
82 Elf_Addr bss_vaddr;
83 Elf_Addr bss_vlimit;
84 caddr_t bss_addr;
85
1c76efe5
MD
86 hdr = get_elf_header(fd, path);
87 if (hdr == NULL)
984263bc 88 return NULL;
984263bc
MD
89
90 /*
91 * Scan the program header entries, and save key information.
92 *
93 * We rely on there being exactly two load segments, text and data,
94 * in that order.
95 */
1c76efe5
MD
96 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
97 phlimit = phdr + hdr->e_phnum;
984263bc
MD
98 nsegs = -1;
99 phdyn = phphdr = phinterp = NULL;
1c76efe5 100 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
984263bc
MD
101 while (phdr < phlimit) {
102 switch (phdr->p_type) {
103
104 case PT_INTERP:
105 phinterp = phdr;
106 break;
107
108 case PT_LOAD:
109 segs[++nsegs] = phdr;
110 if (segs[nsegs]->p_align < PAGE_SIZE) {
111 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
112 path, nsegs);
113 return NULL;
114 }
115 break;
116
117 case PT_PHDR:
118 phphdr = phdr;
119 break;
120
121 case PT_DYNAMIC:
122 phdyn = phdr;
123 break;
124 }
125
126 ++phdr;
127 }
128 if (phdyn == NULL) {
129 _rtld_error("%s: object is not dynamically-linked", path);
130 return NULL;
131 }
132
133 if (nsegs < 0) {
134 _rtld_error("%s: too few PT_LOAD segments", path);
135 return NULL;
136 }
137
138 /*
139 * Map the entire address space of the object, to stake out our
140 * contiguous region, and to establish the base address for relocation.
141 */
142 base_offset = trunc_page(segs[0]->p_offset);
143 base_vaddr = trunc_page(segs[0]->p_vaddr);
144 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
145 mapsize = base_vlimit - base_vaddr;
1c76efe5 146 base_addr = hdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
984263bc
MD
147
148 mapbase = mmap(base_addr, mapsize, convert_prot(segs[0]->p_flags),
149 convert_flags(segs[0]->p_flags), fd, base_offset);
150 if (mapbase == (caddr_t) -1) {
151 _rtld_error("%s: mmap of entire address space failed: %s",
152 path, strerror(errno));
153 return NULL;
154 }
155 if (base_addr != NULL && mapbase != base_addr) {
156 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
157 path, base_addr, mapbase);
158 munmap(mapbase, mapsize);
159 return NULL;
160 }
161
162 for (i = 0; i <= nsegs; i++) {
163 /* Overlay the segment onto the proper region. */
164 data_offset = trunc_page(segs[i]->p_offset);
165 data_vaddr = trunc_page(segs[i]->p_vaddr);
166 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
167 data_addr = mapbase + (data_vaddr - base_vaddr);
168 data_prot = convert_prot(segs[i]->p_flags);
169 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
170 /* Do not call mmap on the first segment - this is redundant */
171 if (i && mmap(data_addr, data_vlimit - data_vaddr, data_prot,
172 data_flags, fd, data_offset) == (caddr_t) -1) {
173 _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
174 return NULL;
175 }
176
177 /* Clear any BSS in the last page of the segment. */
178 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
179 clear_addr = mapbase + (clear_vaddr - base_vaddr);
180 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
181 if ((nclear = data_vlimit - clear_vaddr) > 0) {
182 /* Make sure the end of the segment is writable */
183 if ((data_prot & PROT_WRITE) == 0 &&
184 -1 == mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
185 _rtld_error("%s: mprotect failed: %s", path,
186 strerror(errno));
187 return NULL;
188 }
189
190 memset(clear_addr, 0, nclear);
191
192 /* Reset the data protection back */
193 if ((data_prot & PROT_WRITE) == 0)
194 mprotect(clear_page, PAGE_SIZE, data_prot);
195 }
196
197 /* Overlay the BSS segment onto the proper region. */
198 bss_vaddr = data_vlimit;
199 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
200 bss_addr = mapbase + (bss_vaddr - base_vaddr);
201 if (bss_vlimit > bss_vaddr) { /* There is something to do */
202 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
203 MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
204 _rtld_error("%s: mmap of bss failed: %s", path,
205 strerror(errno));
206 return NULL;
207 }
208 }
209 }
210
211 obj = obj_new();
212 if (sb != NULL) {
213 obj->dev = sb->st_dev;
214 obj->ino = sb->st_ino;
215 }
216 obj->mapbase = mapbase;
217 obj->mapsize = mapsize;
218 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
219 base_vaddr;
220 obj->vaddrbase = base_vaddr;
221 obj->relocbase = mapbase - base_vaddr;
222 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
1c76efe5
MD
223 if (hdr->e_entry != 0)
224 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
984263bc
MD
225 if (phphdr != NULL) {
226 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
227 obj->phsize = phphdr->p_memsz;
228 }
229 if (phinterp != NULL)
230 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
231
232 return obj;
233}
234
1c76efe5
MD
235static Elf_Ehdr *
236get_elf_header (int fd, const char *path)
237{
238 static union {
239 Elf_Ehdr hdr;
240 char buf[PAGE_SIZE];
241 } u;
242 ssize_t nbytes;
243
244 if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
245 _rtld_error("%s: read error: %s", path, strerror(errno));
246 return NULL;
247 }
248
249 /* Make sure the file is valid */
250 if (nbytes < (ssize_t)sizeof(Elf_Ehdr) || !IS_ELF(u.hdr)) {
251 _rtld_error("%s: invalid file format", path);
252 return NULL;
253 }
254 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
255 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
256 _rtld_error("%s: unsupported file layout", path);
257 return NULL;
258 }
259 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
260 || u.hdr.e_version != EV_CURRENT) {
261 _rtld_error("%s: unsupported file version", path);
262 return NULL;
263 }
264 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
265 _rtld_error("%s: unsupported file type", path);
266 return NULL;
267 }
268 if (u.hdr.e_machine != ELF_TARG_MACH) {
269 _rtld_error("%s: unsupported machine", path);
270 return NULL;
271 }
272
273 /*
274 * We rely on the program header being in the first page. This is
275 * not strictly required by the ABI specification, but it seems to
276 * always true in practice. And, it simplifies things considerably.
277 */
278 if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
279 _rtld_error(
280 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
281 return NULL;
282 }
283 if (u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) > (size_t)nbytes) {
284 _rtld_error("%s: program header too large", path);
285 return NULL;
286 }
287
288 return (&u.hdr);
289}
290
984263bc
MD
291void
292obj_free(Obj_Entry *obj)
293{
294 Objlist_Entry *elm;
295
296 free(obj->path);
297 while (obj->needed != NULL) {
298 Needed_Entry *needed = obj->needed;
299 obj->needed = needed->next;
300 free(needed);
301 }
302 while (!STAILQ_EMPTY(&obj->dldags)) {
303 elm = STAILQ_FIRST(&obj->dldags);
304 STAILQ_REMOVE_HEAD(&obj->dldags, link);
305 free(elm);
306 }
307 while (!STAILQ_EMPTY(&obj->dagmembers)) {
308 elm = STAILQ_FIRST(&obj->dagmembers);
309 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
310 free(elm);
311 }
312 free(obj);
313}
314
315Obj_Entry *
316obj_new(void)
317{
318 Obj_Entry *obj;
319
320 obj = CNEW(Obj_Entry);
321 STAILQ_INIT(&obj->dldags);
322 STAILQ_INIT(&obj->dagmembers);
323 return obj;
324}
325
326/*
327 * Given a set of ELF protection flags, return the corresponding protection
328 * flags for MMAP.
329 */
330static int
331convert_prot(int elfflags)
332{
333 int prot = 0;
334 if (elfflags & PF_R)
335 prot |= PROT_READ;
336 if (elfflags & PF_W)
337 prot |= PROT_WRITE;
338 if (elfflags & PF_X)
339 prot |= PROT_EXEC;
340 return prot;
341}
342
343static int
344convert_flags(int elfflags)
345{
346 int flags = MAP_PRIVATE; /* All mappings are private */
347
348 /*
349 * Readonly mappings are marked "MAP_NOCORE", because they can be
350 * reconstructed by a debugger.
351 */
352 if (!(elfflags & PF_W))
353 flags |= MAP_NOCORE;
354 return flags;
355}