map_object.c revision 1.4 1 /* $NetBSD: map_object.c,v 1.4 1999/03/01 16:40:07 christos 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 caddr_t base_addr;
81 Elf_Off data_offset;
82 Elf_Addr data_vaddr;
83 Elf_Addr data_vlimit;
84 caddr_t data_addr;
85 #ifdef RTLD_LOADER
86 Elf_Addr clear_vaddr;
87 caddr_t clear_addr;
88 size_t nclear;
89 Elf_Addr bss_vaddr;
90 Elf_Addr bss_vlimit;
91 caddr_t bss_addr;
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, to stake out our
181 * contiguous region, and to establish the base address for relocation.
182 */
183 base_offset = round_down(segs[0]->p_offset);
184 base_vaddr = round_down(segs[0]->p_vaddr);
185 base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
186 mapsize = base_vlimit - base_vaddr;
187 #ifdef RTLD_LOADER
188 base_addr = u.hdr.e_type == Elf_et_exec ? (caddr_t) base_vaddr : NULL;
189 #else
190 base_addr = NULL;
191 #endif
192
193 mapbase = mmap(base_addr, mapsize, protflags(segs[0]->p_flags),
194 MAP_FILE | MAP_PRIVATE, fd, base_offset);
195 if (mapbase == (caddr_t) - 1) {
196 _rtld_error("mmap of entire address space failed: %s",
197 xstrerror(errno));
198 return NULL;
199 }
200 if (base_addr != NULL && mapbase != base_addr) {
201 _rtld_error("mmap returned wrong address: wanted %p, got %p", base_addr,
202 mapbase);
203 munmap(mapbase, mapsize);
204 return NULL;
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), MAP_FILE | MAP_PRIVATE | MAP_FIXED,
213 fd, data_offset) == (caddr_t)-1) {
214 _rtld_error("mmap of data failed: %s", xstrerror(errno));
215 return NULL;
216 }
217 #ifdef RTLD_LOADER
218 /* Clear any BSS in the last page of the data segment. */
219 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
220 clear_addr = mapbase + (clear_vaddr - base_vaddr);
221 if ((nclear = data_vlimit - clear_vaddr) > 0)
222 memset(clear_addr, 0, nclear);
223
224 /* Overlay the BSS segment onto the proper region. */
225 bss_vaddr = data_vlimit;
226 bss_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
227 bss_addr = mapbase + (bss_vaddr - base_vaddr);
228 if (bss_vlimit > bss_vaddr) { /* There is something to do */
229 if (mmap(bss_addr, bss_vlimit - bss_vaddr,
230 protflags(segs[1]->p_flags),
231 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1,
232 0) == (caddr_t)-1) {
233 _rtld_error("mmap of bss failed: %s", xstrerror(errno));
234 return NULL;
235 }
236 }
237 #endif
238
239 obj = CNEW(Obj_Entry);
240 obj->mapbase = mapbase;
241 obj->mapsize = mapsize;
242 obj->textsize = round_up(segs[0]->p_vaddr + segs[0]->p_memsz) -
243 base_vaddr;
244 obj->vaddrbase = base_vaddr;
245 obj->relocbase = mapbase - base_vaddr;
246 obj->dynamic = (Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr);
247 if (u.hdr.e_entry != 0)
248 obj->entry = (caddr_t)(obj->relocbase + u.hdr.e_entry);
249 if (phphdr != NULL) {
250 obj->phdr = (const Elf_Phdr *)
251 (obj->relocbase + phphdr->p_vaddr);
252 obj->phsize = phphdr->p_memsz;
253 }
254 return obj;
255 }
256
257 /*
258 * Given a set of ELF protection flags, return the corresponding protection
259 * flags for MMAP.
260 */
261 static int
262 protflags(elfflags)
263 int elfflags;
264 {
265 int prot = 0;
266 if (elfflags & Elf_pf_r)
267 prot |= PROT_READ;
268 #ifdef RTLD_LOADER
269 if (elfflags & Elf_pf_w)
270 prot |= PROT_WRITE;
271 #endif
272 if (elfflags & Elf_pf_x)
273 prot |= PROT_EXEC;
274 return prot;
275 }
276