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