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