rtld(1), headers, manual pages: Bring in some whitespace, comment etc. fixes.
[dragonfly.git] / libexec / rtld-elf / map_object.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright 1996-1998 John D. Polstra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD: src/libexec/rtld-elf/map_object.c,v 1.7.2.2 2002/12/28 19:49:41 dillon Exp $
26 */
27
28#include <sys/param.h>
29#include <sys/mman.h>
30#include <sys/stat.h>
31
32#include <errno.h>
33#include <stddef.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38#include "rtld.h"
39
1c76efe5 40static Elf_Ehdr *get_elf_header (int, const char *);
984263bc
MD
41static int convert_prot(int); /* Elf flags -> mmap protection */
42static int convert_flags(int); /* Elf flags -> mmap flags */
43
44/*
45 * Map a shared object into memory. The "fd" argument is a file descriptor,
46 * which must be open on the object and positioned at its beginning.
47 * The "path" argument is a pathname that is used only for error messages.
48 *
49 * The return value is a pointer to a newly-allocated Obj_Entry structure
50 * for the shared object. Returns NULL on failure.
51 */
52Obj_Entry *
53map_object(int fd, const char *path, const struct stat *sb)
54{
55 Obj_Entry *obj;
1c76efe5
MD
56 Elf_Ehdr *hdr;
57 int i;
984263bc
MD
58 Elf_Phdr *phdr;
59 Elf_Phdr *phlimit;
60 Elf_Phdr **segs;
61 int nsegs;
62 Elf_Phdr *phdyn;
63 Elf_Phdr *phphdr;
64 Elf_Phdr *phinterp;
55b88cae 65 Elf_Phdr *phtls;
984263bc
MD
66 caddr_t mapbase;
67 size_t mapsize;
68 Elf_Off base_offset;
69 Elf_Addr base_vaddr;
70 Elf_Addr base_vlimit;
71 caddr_t base_addr;
72 Elf_Off data_offset;
73 Elf_Addr data_vaddr;
74 Elf_Addr data_vlimit;
75 caddr_t data_addr;
76 int data_prot;
77 int data_flags;
78 Elf_Addr clear_vaddr;
79 caddr_t clear_addr;
80 caddr_t clear_page;
81 size_t nclear;
82 Elf_Addr bss_vaddr;
83 Elf_Addr bss_vlimit;
84 caddr_t bss_addr;
85
1c76efe5
MD
86 hdr = get_elf_header(fd, path);
87 if (hdr == NULL)
984263bc 88 return NULL;
984263bc
MD
89
90 /*
91 * Scan the program header entries, and save key information.
92 *
93 * We rely on there being exactly two load segments, text and data,
94 * in that order.
95 */
1c76efe5
MD
96 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
97 phlimit = phdr + hdr->e_phnum;
984263bc 98 nsegs = -1;
55b88cae 99 phdyn = phphdr = phinterp = phtls = NULL;
1c76efe5 100 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
984263bc
MD
101 while (phdr < phlimit) {
102 switch (phdr->p_type) {
103
104 case PT_INTERP:
105 phinterp = phdr;
106 break;
107
108 case PT_LOAD:
109 segs[++nsegs] = phdr;
110 if (segs[nsegs]->p_align < PAGE_SIZE) {
111 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
112 path, nsegs);
113 return NULL;
114 }
115 break;
116
117 case PT_PHDR:
118 phphdr = phdr;
119 break;
120
121 case PT_DYNAMIC:
122 phdyn = phdr;
123 break;
139b8f34 124
55b88cae
DX
125 case PT_TLS:
126 phtls = phdr;
127 break;
984263bc
MD
128 }
129
130 ++phdr;
131 }
132 if (phdyn == NULL) {
133 _rtld_error("%s: object is not dynamically-linked", path);
134 return NULL;
135 }
136
137 if (nsegs < 0) {
138 _rtld_error("%s: too few PT_LOAD segments", path);
139 return NULL;
140 }
141
142 /*
143 * Map the entire address space of the object, to stake out our
144 * contiguous region, and to establish the base address for relocation.
145 */
146 base_offset = trunc_page(segs[0]->p_offset);
147 base_vaddr = trunc_page(segs[0]->p_vaddr);
148 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
149 mapsize = base_vlimit - base_vaddr;
1c76efe5 150 base_addr = hdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
984263bc
MD
151
152 mapbase = mmap(base_addr, mapsize, convert_prot(segs[0]->p_flags),
153 convert_flags(segs[0]->p_flags), fd, base_offset);
154 if (mapbase == (caddr_t) -1) {
155 _rtld_error("%s: mmap of entire address space failed: %s",
156 path, strerror(errno));
157 return NULL;
158 }
159 if (base_addr != NULL && mapbase != base_addr) {
160 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
161 path, base_addr, mapbase);
162 munmap(mapbase, mapsize);
163 return NULL;
164 }
165
166 for (i = 0; i <= nsegs; i++) {
167 /* Overlay the segment onto the proper region. */
168 data_offset = trunc_page(segs[i]->p_offset);
169 data_vaddr = trunc_page(segs[i]->p_vaddr);
170 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
171 data_addr = mapbase + (data_vaddr - base_vaddr);
172 data_prot = convert_prot(segs[i]->p_flags);
173 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
174 /* Do not call mmap on the first segment - this is redundant */
175 if (i && mmap(data_addr, data_vlimit - data_vaddr, data_prot,
176 data_flags, fd, data_offset) == (caddr_t) -1) {
177 _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
178 return NULL;
179 }
180
181 /* Clear any BSS in the last page of the segment. */
182 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
183 clear_addr = mapbase + (clear_vaddr - base_vaddr);
184 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
185 if ((nclear = data_vlimit - clear_vaddr) > 0) {
186 /* Make sure the end of the segment is writable */
4b89341e
MD
187 if ((data_prot & PROT_WRITE) == 0) {
188 if (mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE) < 0) {
984263bc
MD
189 _rtld_error("%s: mprotect failed: %s", path,
190 strerror(errno));
191 return NULL;
4b89341e 192 }
984263bc
MD
193 }
194
195 memset(clear_addr, 0, nclear);
196
4b89341e
MD
197 /*
198 * reset the data protection back, enable the segment to be
199 * coredumped since we modified it.
200 */
201 if ((data_prot & PROT_WRITE) == 0) {
202 madvise(clear_page, PAGE_SIZE, MADV_CORE);
203 mprotect(clear_page, PAGE_SIZE, data_prot);
204 }
984263bc
MD
205 }
206
207 /* Overlay the BSS segment onto the proper region. */
208 bss_vaddr = data_vlimit;
209 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
210 bss_addr = mapbase + (bss_vaddr - base_vaddr);
211 if (bss_vlimit > bss_vaddr) { /* There is something to do */
212 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
213 MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
214 _rtld_error("%s: mmap of bss failed: %s", path,
215 strerror(errno));
216 return NULL;
217 }
218 }
219 }
220
221 obj = obj_new();
222 if (sb != NULL) {
223 obj->dev = sb->st_dev;
224 obj->ino = sb->st_ino;
225 }
226 obj->mapbase = mapbase;
227 obj->mapsize = mapsize;
228 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
229 base_vaddr;
230 obj->vaddrbase = base_vaddr;
231 obj->relocbase = mapbase - base_vaddr;
232 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
1c76efe5
MD
233 if (hdr->e_entry != 0)
234 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
984263bc
MD
235 if (phphdr != NULL) {
236 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
237 obj->phsize = phphdr->p_memsz;
238 }
239 if (phinterp != NULL)
240 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
55b88cae
DX
241 if (phtls != NULL) {
242 tls_dtv_generation++;
243 obj->tlsindex = ++tls_max_index;
244 obj->tlssize = phtls->p_memsz;
245 obj->tlsalign = phtls->p_align;
246 obj->tlsinitsize = phtls->p_filesz;
247 obj->tlsinit = mapbase + phtls->p_vaddr;
248 }
984263bc
MD
249 return obj;
250}
251
1c76efe5
MD
252static Elf_Ehdr *
253get_elf_header (int fd, const char *path)
254{
255 static union {
256 Elf_Ehdr hdr;
257 char buf[PAGE_SIZE];
258 } u;
259 ssize_t nbytes;
260
261 if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
262 _rtld_error("%s: read error: %s", path, strerror(errno));
263 return NULL;
264 }
265
266 /* Make sure the file is valid */
267 if (nbytes < (ssize_t)sizeof(Elf_Ehdr) || !IS_ELF(u.hdr)) {
268 _rtld_error("%s: invalid file format", path);
269 return NULL;
270 }
271 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
272 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
273 _rtld_error("%s: unsupported file layout", path);
274 return NULL;
275 }
276 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
277 || u.hdr.e_version != EV_CURRENT) {
278 _rtld_error("%s: unsupported file version", path);
279 return NULL;
280 }
281 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
282 _rtld_error("%s: unsupported file type", path);
283 return NULL;
284 }
285 if (u.hdr.e_machine != ELF_TARG_MACH) {
286 _rtld_error("%s: unsupported machine", path);
287 return NULL;
288 }
289
290 /*
291 * We rely on the program header being in the first page. This is
292 * not strictly required by the ABI specification, but it seems to
293 * always true in practice. And, it simplifies things considerably.
294 */
295 if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
296 _rtld_error(
297 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
298 return NULL;
299 }
300 if (u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) > (size_t)nbytes) {
301 _rtld_error("%s: program header too large", path);
302 return NULL;
303 }
304
305 return (&u.hdr);
306}
307
984263bc
MD
308void
309obj_free(Obj_Entry *obj)
310{
311 Objlist_Entry *elm;
312
55b88cae
DX
313 if (obj->tls_done) {
314 free_tls_offset(obj);
315 }
167f7029 316 free(obj->origin_path);
984263bc
MD
317 free(obj->path);
318 while (obj->needed != NULL) {
319 Needed_Entry *needed = obj->needed;
320 obj->needed = needed->next;
321 free(needed);
322 }
323 while (!STAILQ_EMPTY(&obj->dldags)) {
324 elm = STAILQ_FIRST(&obj->dldags);
325 STAILQ_REMOVE_HEAD(&obj->dldags, link);
326 free(elm);
327 }
328 while (!STAILQ_EMPTY(&obj->dagmembers)) {
329 elm = STAILQ_FIRST(&obj->dagmembers);
330 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
331 free(elm);
332 }
333 free(obj);
334}
335
336Obj_Entry *
337obj_new(void)
338{
339 Obj_Entry *obj;
340
341 obj = CNEW(Obj_Entry);
342 STAILQ_INIT(&obj->dldags);
343 STAILQ_INIT(&obj->dagmembers);
344 return obj;
345}
346
347/*
348 * Given a set of ELF protection flags, return the corresponding protection
349 * flags for MMAP.
350 */
351static int
352convert_prot(int elfflags)
353{
354 int prot = 0;
355 if (elfflags & PF_R)
356 prot |= PROT_READ;
357 if (elfflags & PF_W)
358 prot |= PROT_WRITE;
359 if (elfflags & PF_X)
360 prot |= PROT_EXEC;
361 return prot;
362}
363
364static int
365convert_flags(int elfflags)
366{
367 int flags = MAP_PRIVATE; /* All mappings are private */
368
369 /*
370 * Readonly mappings are marked "MAP_NOCORE", because they can be
371 * reconstructed by a debugger.
372 */
373 if (!(elfflags & PF_W))
374 flags |= MAP_NOCORE;
375 return flags;
376}