rtld: Parse notes later (bug# 2376)
[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 *
f4f4bfd5 25 * $FreeBSD$
984263bc
MD
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
fcf53d9b 38#include "debug.h"
984263bc
MD
39#include "rtld.h"
40
1c76efe5 41static Elf_Ehdr *get_elf_header (int, const char *);
984263bc
MD
42static int convert_prot(int); /* Elf flags -> mmap protection */
43static int convert_flags(int); /* Elf flags -> mmap flags */
44
45/*
46 * Map a shared object into memory. The "fd" argument is a file descriptor,
47 * which must be open on the object and positioned at its beginning.
48 * The "path" argument is a pathname that is used only for error messages.
49 *
50 * The return value is a pointer to a newly-allocated Obj_Entry structure
51 * for the shared object. Returns NULL on failure.
52 */
53Obj_Entry *
54map_object(int fd, const char *path, const struct stat *sb)
55{
56 Obj_Entry *obj;
1c76efe5
MD
57 Elf_Ehdr *hdr;
58 int i;
984263bc
MD
59 Elf_Phdr *phdr;
60 Elf_Phdr *phlimit;
61 Elf_Phdr **segs;
62 int nsegs;
63 Elf_Phdr *phdyn;
984263bc 64 Elf_Phdr *phinterp;
55b88cae 65 Elf_Phdr *phtls;
984263bc
MD
66 caddr_t mapbase;
67 size_t mapsize;
984263bc
MD
68 Elf_Addr base_vaddr;
69 Elf_Addr base_vlimit;
70 caddr_t base_addr;
71 Elf_Off data_offset;
72 Elf_Addr data_vaddr;
73 Elf_Addr data_vlimit;
74 caddr_t data_addr;
75 int data_prot;
76 int data_flags;
77 Elf_Addr clear_vaddr;
78 caddr_t clear_addr;
79 caddr_t clear_page;
fcf53d9b
JM
80 Elf_Addr phdr_vaddr;
81 size_t nclear, phsize;
984263bc
MD
82 Elf_Addr bss_vaddr;
83 Elf_Addr bss_vlimit;
84 caddr_t bss_addr;
e9de6dcc 85 Elf_Word stack_flags;
007f494e
JM
86 Elf_Addr relro_page;
87 size_t relro_size;
7f5c8f97
JM
88 Elf_Addr note_start;
89 Elf_Addr note_end;
984263bc 90
1c76efe5
MD
91 hdr = get_elf_header(fd, path);
92 if (hdr == NULL)
fcf53d9b 93 return (NULL);
984263bc
MD
94
95 /*
96 * Scan the program header entries, and save key information.
97 *
fcf53d9b 98 * We expect that the loadable segments are ordered by load address.
984263bc 99 */
1c76efe5 100 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
fcf53d9b 101 phsize = hdr->e_phnum * sizeof (phdr[0]);
1c76efe5 102 phlimit = phdr + hdr->e_phnum;
984263bc 103 nsegs = -1;
fcf53d9b
JM
104 phdyn = phinterp = phtls = NULL;
105 phdr_vaddr = 0;
007f494e
JM
106 relro_page = 0;
107 relro_size = 0;
7f5c8f97
JM
108 note_start = 0;
109 note_end = 0;
1c76efe5 110 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
e9de6dcc 111 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
984263bc
MD
112 while (phdr < phlimit) {
113 switch (phdr->p_type) {
114
115 case PT_INTERP:
116 phinterp = phdr;
117 break;
118
119 case PT_LOAD:
120 segs[++nsegs] = phdr;
fcf53d9b 121 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
984263bc
MD
122 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
123 path, nsegs);
124 return NULL;
125 }
126 break;
127
128 case PT_PHDR:
fcf53d9b
JM
129 phdr_vaddr = phdr->p_vaddr;
130 phsize = phdr->p_memsz;
984263bc
MD
131 break;
132
133 case PT_DYNAMIC:
134 phdyn = phdr;
135 break;
139b8f34 136
55b88cae
DX
137 case PT_TLS:
138 phtls = phdr;
139 break;
007f494e 140
e9de6dcc
JM
141 case PT_GNU_STACK:
142 stack_flags = phdr->p_flags;
143 break;
144
007f494e
JM
145 case PT_GNU_RELRO:
146 relro_page = phdr->p_vaddr;
147 relro_size = phdr->p_memsz;
148 break;
7f5c8f97
JM
149
150 case PT_NOTE:
151 if (phdr->p_offset > PAGE_SIZE ||
152 phdr->p_offset + phdr->p_filesz > PAGE_SIZE)
153 break;
154 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
155 note_end = note_start + phdr->p_filesz;
7f5c8f97 156 break;
984263bc
MD
157 }
158
159 ++phdr;
160 }
161 if (phdyn == NULL) {
162 _rtld_error("%s: object is not dynamically-linked", path);
163 return NULL;
164 }
165
166 if (nsegs < 0) {
167 _rtld_error("%s: too few PT_LOAD segments", path);
168 return NULL;
169 }
170
171 /*
172 * Map the entire address space of the object, to stake out our
173 * contiguous region, and to establish the base address for relocation.
174 */
984263bc
MD
175 base_vaddr = trunc_page(segs[0]->p_vaddr);
176 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
177 mapsize = base_vlimit - base_vaddr;
1c76efe5 178 base_addr = hdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
984263bc 179
fcf53d9b
JM
180 mapbase = mmap(base_addr, mapsize, PROT_NONE, MAP_ANON | MAP_PRIVATE |
181 MAP_NOCORE, -1, 0);
984263bc
MD
182 if (mapbase == (caddr_t) -1) {
183 _rtld_error("%s: mmap of entire address space failed: %s",
b2cf2649 184 path, rtld_strerror(errno));
984263bc
MD
185 return NULL;
186 }
187 if (base_addr != NULL && mapbase != base_addr) {
188 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
189 path, base_addr, mapbase);
190 munmap(mapbase, mapsize);
191 return NULL;
192 }
193
fcf53d9b 194 for (i = 0; i <= nsegs; i++) {
984263bc
MD
195 /* Overlay the segment onto the proper region. */
196 data_offset = trunc_page(segs[i]->p_offset);
197 data_vaddr = trunc_page(segs[i]->p_vaddr);
198 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
199 data_addr = mapbase + (data_vaddr - base_vaddr);
200 data_prot = convert_prot(segs[i]->p_flags);
201 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
fcf53d9b 202 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
984263bc 203 data_flags, fd, data_offset) == (caddr_t) -1) {
b2cf2649
JM
204 _rtld_error("%s: mmap of data failed: %s", path,
205 rtld_strerror(errno));
984263bc
MD
206 return NULL;
207 }
208
fcf53d9b
JM
209 /* Do BSS setup */
210 if (segs[i]->p_filesz != segs[i]->p_memsz) {
211
212 /* Clear any BSS in the last page of the segment. */
213 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
214 clear_addr = mapbase + (clear_vaddr - base_vaddr);
215 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
216
217 if ((nclear = data_vlimit - clear_vaddr) > 0) {
218 /* Make sure the end of the segment is writable */
9b53619b
JM
219 if ((data_prot & PROT_WRITE) == 0 && -1 ==
220 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
984263bc 221 _rtld_error("%s: mprotect failed: %s", path,
b2cf2649 222 rtld_strerror(errno));
984263bc 223 return NULL;
4b89341e 224 }
984263bc 225
fcf53d9b 226 memset(clear_addr, 0, nclear);
984263bc 227
fcf53d9b
JM
228 /*
229 * reset the data protection back, enable the segment to be
230 * coredumped since we modified it.
231 */
232 if ((data_prot & PROT_WRITE) == 0) {
233 madvise(clear_page, PAGE_SIZE, MADV_CORE);
234 mprotect(clear_page, PAGE_SIZE, data_prot);
235 }
4b89341e 236 }
984263bc 237
fcf53d9b
JM
238 /* Overlay the BSS segment onto the proper region. */
239 bss_vaddr = data_vlimit;
240 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
241 bss_addr = mapbase + (bss_vaddr - base_vaddr);
242 if (bss_vlimit > bss_vaddr) { /* There is something to do */
55071f75
JM
243 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
244 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
245 _rtld_error("%s: mmap of bss failed: %s", path,
b2cf2649 246 rtld_strerror(errno));
fcf53d9b
JM
247 return NULL;
248 }
984263bc
MD
249 }
250 }
fcf53d9b
JM
251
252 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
253 (data_vlimit - data_vaddr + data_offset) >=
254 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
255 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
256 }
984263bc
MD
257 }
258
259 obj = obj_new();
260 if (sb != NULL) {
261 obj->dev = sb->st_dev;
262 obj->ino = sb->st_ino;
263 }
264 obj->mapbase = mapbase;
265 obj->mapsize = mapsize;
266 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
267 base_vaddr;
268 obj->vaddrbase = base_vaddr;
269 obj->relocbase = mapbase - base_vaddr;
270 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
1c76efe5
MD
271 if (hdr->e_entry != 0)
272 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
fcf53d9b
JM
273 if (phdr_vaddr != 0) {
274 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr);
275 } else {
276 obj->phdr = malloc(phsize);
277 if (obj->phdr == NULL) {
278 obj_free(obj);
279 _rtld_error("%s: cannot allocate program header", path);
280 return NULL;
281 }
282 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize);
283 obj->phdr_alloc = true;
984263bc 284 }
fcf53d9b 285 obj->phsize = phsize;
984263bc
MD
286 if (phinterp != NULL)
287 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
55b88cae
DX
288 if (phtls != NULL) {
289 tls_dtv_generation++;
290 obj->tlsindex = ++tls_max_index;
291 obj->tlssize = phtls->p_memsz;
292 obj->tlsalign = phtls->p_align;
293 obj->tlsinitsize = phtls->p_filesz;
294 obj->tlsinit = mapbase + phtls->p_vaddr;
295 }
e9de6dcc 296 obj->stack_flags = stack_flags;
007f494e
JM
297 if (relro_size) {
298 obj->relro_page = obj->relocbase + trunc_page(relro_page);
299 obj->relro_size = round_page(relro_size);
300 }
4db7b266
JM
301 if (note_start < note_end)
302 digest_notes(obj, note_start, note_end);
984263bc
MD
303 return obj;
304}
305
1c76efe5
MD
306static Elf_Ehdr *
307get_elf_header (int fd, const char *path)
308{
309 static union {
310 Elf_Ehdr hdr;
311 char buf[PAGE_SIZE];
312 } u;
313 ssize_t nbytes;
314
fcf53d9b 315 if ((nbytes = pread(fd, u.buf, PAGE_SIZE, 0)) == -1) {
b2cf2649 316 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
1c76efe5
MD
317 return NULL;
318 }
319
320 /* Make sure the file is valid */
321 if (nbytes < (ssize_t)sizeof(Elf_Ehdr) || !IS_ELF(u.hdr)) {
322 _rtld_error("%s: invalid file format", path);
323 return NULL;
324 }
325 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
326 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
327 _rtld_error("%s: unsupported file layout", path);
328 return NULL;
329 }
330 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
331 || u.hdr.e_version != EV_CURRENT) {
332 _rtld_error("%s: unsupported file version", path);
333 return NULL;
334 }
335 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
336 _rtld_error("%s: unsupported file type", path);
337 return NULL;
338 }
339 if (u.hdr.e_machine != ELF_TARG_MACH) {
340 _rtld_error("%s: unsupported machine", path);
341 return NULL;
342 }
343
344 /*
345 * We rely on the program header being in the first page. This is
346 * not strictly required by the ABI specification, but it seems to
347 * always true in practice. And, it simplifies things considerably.
348 */
349 if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
350 _rtld_error(
351 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
352 return NULL;
353 }
354 if (u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) > (size_t)nbytes) {
355 _rtld_error("%s: program header too large", path);
356 return NULL;
357 }
358
359 return (&u.hdr);
360}
361
984263bc
MD
362void
363obj_free(Obj_Entry *obj)
364{
365 Objlist_Entry *elm;
366
fcf53d9b 367 if (obj->tls_done)
55b88cae 368 free_tls_offset(obj);
984263bc
MD
369 while (obj->needed != NULL) {
370 Needed_Entry *needed = obj->needed;
371 obj->needed = needed->next;
372 free(needed);
373 }
fcf53d9b
JM
374 while (!STAILQ_EMPTY(&obj->names)) {
375 Name_Entry *entry = STAILQ_FIRST(&obj->names);
376 STAILQ_REMOVE_HEAD(&obj->names, link);
377 free(entry);
378 }
984263bc
MD
379 while (!STAILQ_EMPTY(&obj->dldags)) {
380 elm = STAILQ_FIRST(&obj->dldags);
381 STAILQ_REMOVE_HEAD(&obj->dldags, link);
382 free(elm);
383 }
384 while (!STAILQ_EMPTY(&obj->dagmembers)) {
385 elm = STAILQ_FIRST(&obj->dagmembers);
386 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
387 free(elm);
388 }
fcf53d9b
JM
389 if (obj->vertab)
390 free(obj->vertab);
391 if (obj->origin_path)
392 free(obj->origin_path);
393 if (obj->z_origin)
394 free(obj->rpath);
395 if (obj->priv)
396 free(obj->priv);
397 if (obj->path)
398 free(obj->path);
399 if (obj->phdr_alloc)
400 free((void *)obj->phdr);
984263bc
MD
401 free(obj);
402}
403
404Obj_Entry *
405obj_new(void)
406{
407 Obj_Entry *obj;
408
409 obj = CNEW(Obj_Entry);
410 STAILQ_INIT(&obj->dldags);
411 STAILQ_INIT(&obj->dagmembers);
fcf53d9b 412 STAILQ_INIT(&obj->names);
984263bc
MD
413 return obj;
414}
415
416/*
417 * Given a set of ELF protection flags, return the corresponding protection
418 * flags for MMAP.
419 */
420static int
421convert_prot(int elfflags)
422{
423 int prot = 0;
424 if (elfflags & PF_R)
425 prot |= PROT_READ;
426 if (elfflags & PF_W)
427 prot |= PROT_WRITE;
428 if (elfflags & PF_X)
429 prot |= PROT_EXEC;
430 return prot;
431}
432
433static int
434convert_flags(int elfflags)
435{
436 int flags = MAP_PRIVATE; /* All mappings are private */
437
438 /*
439 * Readonly mappings are marked "MAP_NOCORE", because they can be
440 * reconstructed by a debugger.
441 */
442 if (!(elfflags & PF_W))
443 flags |= MAP_NOCORE;
444 return flags;
445}