map_object.c revision 1.5 1 /* $NetBSD: map_object.c,v 1.5 1999/08/06 22:33:49 thorpej 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 <string.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/mman.h>
40
41 #include "rtld.h"
42
43 #define CONCAT(x,y) __CONCAT(x,y)
44 #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
45 #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
46 #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
47 #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
48
49 static int protflags __P((int)); /* Elf flags -> mmap protection */
50
51 /*
52 * Map a shared object into memory. The argument is a file descriptor,
53 * which must be open on the object and positioned at its beginning.
54 *
55 * The return value is a pointer to a newly-allocated Obj_Entry structure
56 * for the shared object. Returns NULL on failure.
57 */
58 Obj_Entry *
59 _rtld_map_object(path, fd)
60 const char *path;
61 int fd;
62 {
63 Obj_Entry *obj;
64 union {
65 Elf_Ehdr hdr;
66 char buf[PAGESIZE];
67 } u;
68 int nbytes;
69 Elf_Phdr *phdr;
70 Elf_Phdr *phlimit;
71 Elf_Phdr *segs[2];
72 int nsegs;
73 Elf_Phdr *phdyn;
74 Elf_Phdr *phphdr;
75 caddr_t mapbase;
76 size_t mapsize;
77 Elf_Off base_offset;
78 Elf_Addr base_vaddr;
79 Elf_Addr base_vlimit;
80 Elf_Addr text_vlimit;
81 caddr_t base_addr;
82 Elf_Off data_offset;
83 Elf_Addr data_vaddr;
84 Elf_Addr data_vlimit;
85 caddr_t data_addr;
86 caddr_t gap_addr;
87 size_t gap_size;
88 #ifdef RTLD_LOADER
89 Elf_Addr clear_vaddr;
90 caddr_t clear_addr;
91 size_t nclear;
92 #endif
93
94 if ((nbytes = read(fd, u.buf, PAGESIZE)) == -1) {
95 _rtld_error("%s: read error: %s", path, xstrerror(errno));
96 return NULL;
97 }
98 /* Make sure the file is valid */
99 if (nbytes < sizeof(Elf_Ehdr) ||
100 memcmp(Elf_e_ident, u.hdr.e_ident, Elf_e_siz) != 0) {
101 _rtld_error("%s: unrecognized file format", path);
102 return NULL;
103 }
104 /* Elf_e_ident includes class */
105 if (u.hdr.e_ident[Elf_ei_version] != Elf_ev_current ||
106 u.hdr.e_version != Elf_ev_current ||
107 u.hdr.e_ident[Elf_ei_data] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
108 _rtld_error("%s: Unsupported file version", path);
109 return NULL;
110 }
111 if (u.hdr.e_type != Elf_et_exec && u.hdr.e_type != Elf_et_dyn) {
112 _rtld_error("%s: Unsupported file type", path);
113 return NULL;
114 }
115 switch (u.hdr.e_machine) {
116 ELFDEFNNAME(MACHDEP_ID_CASES)
117 default:
118 _rtld_error("%s: Unsupported machine", path);
119 return NULL;
120 }
121
122 /*
123 * We rely on the program header being in the first page. This is
124 * not strictly required by the ABI specification, but it seems to
125 * always true in practice. And, it simplifies things considerably.
126 */
127 assert(u.hdr.e_phentsize == sizeof(Elf_Phdr));
128 assert(u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) <= PAGESIZE);
129 assert(u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) <= nbytes);
130
131 /*
132 * Scan the program header entries, and save key information.
133 *
134 * We rely on there being exactly two load segments, text and data,
135 * in that order.
136 */
137 phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff);
138 phlimit = phdr + u.hdr.e_phnum;
139 nsegs = 0;
140 phdyn = NULL;
141 phphdr = NULL;
142 while (phdr < phlimit) {
143 switch (phdr->p_type) {
144
145 case Elf_pt_load:
146 #ifdef __mips__
147 /* NetBSD/pmax 1.1 elf toolchain peculiarity */
148 if (nsegs >= 2) {
149 _rtld_error("%s: too many sections\n", path);
150 return NULL;
151 }
152 #endif
153 assert(nsegs < 2);
154 segs[nsegs] = phdr;
155 ++nsegs;
156 break;
157
158 case Elf_pt_phdr:
159 phphdr = phdr;
160 break;
161
162 case Elf_pt_dynamic:
163 phdyn = phdr;
164 break;
165 }
166
167 ++phdr;
168 }
169 if (phdyn == NULL) {
170 _rtld_error("%s: not dynamically-linked", path);
171 return NULL;
172 }
173 assert(nsegs == 2);
174 #ifdef __i386__
175 assert(segs[0]->p_align <= PAGESIZE);
176 assert(segs[1]->p_align <= PAGESIZE);
177 #endif
178
179 /*
180 * Map the entire address space of the object as an anonymous
181 * region to stake out our contiguous region and establish a
182 * base for relocation.
183 *
184 * We map it using the data/BSS protection, then overlay bits
185 * of the file over the top, and unmap the gaps left by padding
186 * to alignment.
187 */
188 base_offset = round_down(segs[0]->p_offset);
189 base_vaddr = round_down(segs[0]->p_vaddr);
190 base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
191 mapsize = base_vlimit - base_vaddr;
192 #ifdef RTLD_LOADER
193 base_addr = u.hdr.e_type == Elf_et_exec ? (caddr_t) base_vaddr : NULL;
194 #else
195 base_addr = NULL;
196 #endif
197
198 mapbase = mmap(base_addr, mapsize, protflags(segs[1]->p_flags),
199 MAP_ANON | MAP_PRIVATE, -1, (off_t) 0);
200 if (mapbase == MAP_FAILED) {
201 _rtld_error("mmap of entire address space failed: %s",
202 xstrerror(errno));
203 return NULL;
204 }
205 if (base_addr != NULL && mapbase != base_addr) {
206 _rtld_error("mmap returned wrong address: wanted %p, got %p",
207 base_addr, mapbase);
208 munmap(mapbase, mapsize);
209 return NULL;
210 }
211 base_addr = mapbase;
212
213 /* Overlay the text segment onto the proper region. */
214 text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
215 if (mmap(base_addr, text_vlimit - base_vaddr,
216 protflags(segs[0]->p_flags), MAP_FILE | MAP_PRIVATE | MAP_FIXED,
217 fd, base_offset) == MAP_FAILED) {
218 _rtld_error("mmap of text failed: %s", xstrerror(errno));
219 return NULL;
220 }
221
222 /* Overlay the data segment onto the proper region. */
223 data_offset = round_down(segs[1]->p_offset);
224 data_vaddr = round_down(segs[1]->p_vaddr);
225 data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
226 data_addr = mapbase + (data_vaddr - base_vaddr);
227 if (mmap(data_addr, data_vlimit - data_vaddr,
228 protflags(segs[1]->p_flags), MAP_FILE | MAP_PRIVATE | MAP_FIXED,
229 fd, data_offset) == MAP_FAILED) {
230 _rtld_error("mmap of data failed: %s", xstrerror(errno));
231 return NULL;
232 }
233
234 /* Unmap the gap between the text and data. */
235 gap_addr = base_addr + round_up(text_vlimit - base_vaddr);
236 gap_size = data_addr - gap_addr;
237 if (gap_size != 0 && munmap(gap_addr, gap_size) == -1) {
238 _rtld_error("munmap of text -> data gap failed: %s",
239 xstrerror(errno));
240 return NULL;
241 }
242
243 #ifdef RTLD_LOADER
244 /* Clear any BSS in the last page of the data segment. */
245 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
246 clear_addr = mapbase + (clear_vaddr - base_vaddr);
247 if ((nclear = data_vlimit - clear_vaddr) > 0)
248 memset(clear_addr, 0, nclear);
249
250 /* Non-file portion of BSS mapped above. */
251 #endif
252
253 obj = CNEW(Obj_Entry);
254 obj->mapbase = mapbase;
255 obj->mapsize = mapsize;
256 obj->textsize = round_up(segs[0]->p_vaddr + segs[0]->p_memsz) -
257 base_vaddr;
258 obj->vaddrbase = base_vaddr;
259 obj->relocbase = mapbase - base_vaddr;
260 obj->dynamic = (Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr);
261 if (u.hdr.e_entry != 0)
262 obj->entry = (caddr_t)(obj->relocbase + u.hdr.e_entry);
263 if (phphdr != NULL) {
264 obj->phdr = (const Elf_Phdr *)
265 (obj->relocbase + phphdr->p_vaddr);
266 obj->phsize = phphdr->p_memsz;
267 }
268 return obj;
269 }
270
271 /*
272 * Given a set of ELF protection flags, return the corresponding protection
273 * flags for MMAP.
274 */
275 static int
276 protflags(elfflags)
277 int elfflags;
278 {
279 int prot = 0;
280 if (elfflags & Elf_pf_r)
281 prot |= PROT_READ;
282 #ifdef RTLD_LOADER
283 if (elfflags & Elf_pf_w)
284 prot |= PROT_WRITE;
285 #endif
286 if (elfflags & Elf_pf_x)
287 prot |= PROT_EXEC;
288 return prot;
289 }
290