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