map_object.c revision 1.19 1 /* $NetBSD: map_object.c,v 1.19 2002/09/24 09:35:13 junyoung 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 void *u;
63 Elf_Phdr *phdr;
64 Elf_Phdr *phlimit;
65 Elf_Phdr *segs[2];
66 int nsegs;
67 Elf_Phdr *phdyn;
68 Elf_Phdr *phphdr;
69 Elf_Phdr *phinterp;
70 caddr_t mapbase;
71 size_t mapsize;
72 Elf_Off base_offset;
73 Elf_Addr base_vaddr;
74 Elf_Addr base_vlimit;
75 Elf_Addr text_vlimit;
76 caddr_t base_addr;
77 Elf_Off data_offset;
78 Elf_Addr data_vaddr;
79 Elf_Addr data_vlimit;
80 caddr_t data_addr;
81 caddr_t gap_addr;
82 size_t gap_size;
83 #ifdef RTLD_LOADER
84 Elf_Addr clear_vaddr;
85 caddr_t clear_addr;
86 size_t nclear;
87 #endif
88
89 u = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
90 (off_t)0);
91 if (u == MAP_FAILED) {
92 _rtld_error("%s: read error: %s", path, xstrerror(errno));
93 return NULL;
94 }
95 ehdr = (Elf_Ehdr *)u;
96 /* Make sure the file is valid */
97 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
98 ehdr->e_ident[EI_CLASS] != ELFCLASS) {
99 _rtld_error("%s: unrecognized file format", path);
100 goto bad;
101 }
102 /* Elf_e_ident includes class */
103 if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
104 ehdr->e_version != EV_CURRENT ||
105 ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
106 _rtld_error("%s: unsupported file version", path);
107 goto bad;
108 }
109 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
110 _rtld_error("%s: unsupported file type", path);
111 goto bad;
112 }
113 switch (ehdr->e_machine) {
114 ELFDEFNNAME(MACHDEP_ID_CASES)
115 default:
116 _rtld_error("%s: unsupported machine", path);
117 goto bad;
118 }
119
120 /*
121 * We rely on the program header being in the first page. This is
122 * not strictly required by the ABI specification, but it seems to
123 * always true in practice. And, it simplifies things considerably.
124 */
125 assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
126 assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
127 _rtld_pagesz);
128
129 /*
130 * Scan the program header entries, and save key information.
131 *
132 * We rely on there being exactly two load segments, text and data,
133 * in that order.
134 */
135 phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
136 phlimit = phdr + ehdr->e_phnum;
137 nsegs = 0;
138 phdyn = phphdr = phinterp = NULL;
139 while (phdr < phlimit) {
140 switch (phdr->p_type) {
141 case PT_INTERP:
142 phinterp = phdr;
143 break;
144
145 case PT_LOAD:
146 if (nsegs < 2)
147 segs[nsegs] = phdr;
148 ++nsegs;
149 break;
150
151 case PT_PHDR:
152 phphdr = phdr;
153 break;
154
155 case PT_DYNAMIC:
156 phdyn = phdr;
157 break;
158 }
159
160 ++phdr;
161 }
162 if (phdyn == NULL) {
163 _rtld_error("%s: not dynamically linked", path);
164 goto bad;
165 }
166 if (nsegs != 2) {
167 _rtld_error("%s: wrong number of segments (%d != 2)", path,
168 nsegs);
169 goto bad;
170 }
171
172 /*
173 * Map the entire address space of the object as a file
174 * region to stake out our contiguous region and establish a
175 * base for relocation. We use a file mapping so that
176 * the kernel will give us whatever alignment is appropriate
177 * for the platform we're running on.
178 *
179 * We map it using the text protection, map the data segment
180 * into the right place, then map an anon segment for the bss
181 * and unmap the gaps left by padding to alignment.
182 */
183
184 base_offset = round_down(segs[0]->p_offset);
185 base_vaddr = round_down(segs[0]->p_vaddr);
186 base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
187 text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
188 mapsize = base_vlimit - base_vaddr;
189
190 #ifdef RTLD_LOADER
191 base_addr = ehdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
192 #else
193 base_addr = NULL;
194 #endif
195
196 mapbase = mmap(base_addr, mapsize, protflags(segs[0]->p_flags),
197 MAP_FILE | MAP_PRIVATE, fd, base_offset);
198 if (mapbase == MAP_FAILED) {
199 _rtld_error("mmap of entire address space failed: %s",
200 xstrerror(errno));
201 goto bad;
202 }
203
204 base_addr = mapbase;
205
206 /* Overlay the data segment onto the proper region. */
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_addr = mapbase + (data_vaddr - base_vaddr);
211 if (mmap(data_addr, data_vlimit - data_vaddr,
212 protflags(segs[1]->p_flags),
213 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset)
214 == MAP_FAILED) {
215 _rtld_error("mmap of data failed: %s", xstrerror(errno));
216 munmap(mapbase, mapsize);
217 goto bad;
218 }
219
220 /* Overlay the bss segment onto the proper region. */
221 if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
222 protflags(segs[1]->p_flags),
223 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0)
224 == MAP_FAILED) {
225 _rtld_error("mmap of bss failed: %s", xstrerror(errno));
226 munmap(mapbase, mapsize);
227 goto bad;
228 }
229
230 /* Unmap the gap between the text and data. */
231 gap_addr = base_addr + round_up(text_vlimit - base_vaddr);
232 gap_size = data_addr - gap_addr;
233 if (gap_size != 0 && munmap(gap_addr, gap_size) == -1) {
234 _rtld_error("munmap of text -> data gap failed: %s",
235 xstrerror(errno));
236 munmap(mapbase, mapsize);
237 goto bad;
238 }
239
240 #ifdef RTLD_LOADER
241 /* Clear any BSS in the last page of the data segment. */
242 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
243 clear_addr = mapbase + (clear_vaddr - base_vaddr);
244 if ((nclear = data_vlimit - clear_vaddr) > 0)
245 memset(clear_addr, 0, nclear);
246
247 /* Non-file portion of BSS mapped above. */
248 #endif
249
250 obj = _rtld_obj_new();
251 obj->path = path;
252 if (sb != NULL) {
253 obj->dev = sb->st_dev;
254 obj->ino = sb->st_ino;
255 }
256 obj->mapbase = mapbase;
257 obj->mapsize = mapsize;
258 obj->textsize = round_up(segs[0]->p_vaddr + segs[0]->p_memsz) -
259 base_vaddr;
260 obj->vaddrbase = base_vaddr;
261 obj->relocbase = mapbase - base_vaddr;
262 obj->dynamic = (Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr);
263 if (ehdr->e_entry != 0)
264 obj->entry = (caddr_t)(obj->relocbase + ehdr->e_entry);
265 if (phphdr != NULL) {
266 obj->phdr = (const Elf_Phdr *)
267 (obj->relocbase + phphdr->p_vaddr);
268 obj->phsize = phphdr->p_memsz;
269 }
270 if (phinterp != NULL)
271 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
272 obj->isdynamic = ehdr->e_type == ET_DYN;
273
274 return obj;
275
276 bad:
277 munmap(u, _rtld_pagesz);
278 return NULL;
279 }
280
281 void
282 _rtld_obj_free(obj)
283 Obj_Entry *obj;
284 {
285 Objlist_Entry *elm;
286
287 free(obj->path);
288 while (obj->needed != NULL) {
289 Needed_Entry *needed = obj->needed;
290 obj->needed = needed->next;
291 free(needed);
292 }
293 while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
294 SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
295 free(elm);
296 }
297 while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
298 SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
299 free(elm);
300 }
301 free(obj);
302 }
303
304 Obj_Entry *
305 _rtld_obj_new(void)
306 {
307 Obj_Entry *obj;
308
309 obj = CNEW(Obj_Entry);
310 SIMPLEQ_INIT(&obj->dldags);
311 SIMPLEQ_INIT(&obj->dagmembers);
312 return obj;
313 }
314
315 /*
316 * Given a set of ELF protection flags, return the corresponding protection
317 * flags for MMAP.
318 */
319 static int
320 protflags(elfflags)
321 int elfflags;
322 {
323 int prot = 0;
324 if (elfflags & PF_R)
325 prot |= PROT_READ;
326 #ifdef RTLD_LOADER
327 if (elfflags & PF_W)
328 prot |= PROT_WRITE;
329 #endif
330 if (elfflags & PF_X)
331 prot |= PROT_EXEC;
332 return prot;
333 }
334