xref: /freebsd/libexec/rtld-elf/map_object.c (revision da9f24547061aaed0e667eeafd3e29e48fc21224)
13124c3e0SJohn Polstra /*-
23124c3e0SJohn Polstra  * Copyright 1996-1998 John D. Polstra.
33124c3e0SJohn Polstra  * All rights reserved.
43124c3e0SJohn Polstra  *
53124c3e0SJohn Polstra  * Redistribution and use in source and binary forms, with or without
63124c3e0SJohn Polstra  * modification, are permitted provided that the following conditions
73124c3e0SJohn Polstra  * are met:
83124c3e0SJohn Polstra  * 1. Redistributions of source code must retain the above copyright
93124c3e0SJohn Polstra  *    notice, this list of conditions and the following disclaimer.
103124c3e0SJohn Polstra  * 2. Redistributions in binary form must reproduce the above copyright
113124c3e0SJohn Polstra  *    notice, this list of conditions and the following disclaimer in the
123124c3e0SJohn Polstra  *    documentation and/or other materials provided with the distribution.
133124c3e0SJohn Polstra  *
143124c3e0SJohn Polstra  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153124c3e0SJohn Polstra  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163124c3e0SJohn Polstra  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
173124c3e0SJohn Polstra  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
183124c3e0SJohn Polstra  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
193124c3e0SJohn Polstra  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203124c3e0SJohn Polstra  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213124c3e0SJohn Polstra  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223124c3e0SJohn Polstra  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
233124c3e0SJohn Polstra  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
243124c3e0SJohn Polstra  *
257f3dea24SPeter Wemm  * $FreeBSD$
263124c3e0SJohn Polstra  */
273124c3e0SJohn Polstra 
283124c3e0SJohn Polstra #include <sys/param.h>
293124c3e0SJohn Polstra #include <sys/mman.h>
307360ae0fSJohn Polstra #include <sys/stat.h>
313124c3e0SJohn Polstra 
323124c3e0SJohn Polstra #include <errno.h>
333124c3e0SJohn Polstra #include <stddef.h>
34926ea445SJohn Polstra #include <stdlib.h>
353124c3e0SJohn Polstra #include <string.h>
363124c3e0SJohn Polstra #include <unistd.h>
373124c3e0SJohn Polstra 
38b5393d9fSDoug Rabson #include "debug.h"
393124c3e0SJohn Polstra #include "rtld.h"
403124c3e0SJohn Polstra 
41341b3de6SMatthew N. Dodd static Elf_Ehdr *get_elf_header (int, const char *);
42fa7dd9c5SMatthew Dillon static int convert_prot(int);	/* Elf flags -> mmap protection */
43fa7dd9c5SMatthew Dillon static int convert_flags(int); /* Elf flags -> mmap flags */
443124c3e0SJohn Polstra 
453124c3e0SJohn Polstra /*
46bfb1ef60SJohn Polstra  * Map a shared object into memory.  The "fd" argument is a file descriptor,
473124c3e0SJohn Polstra  * which must be open on the object and positioned at its beginning.
48bfb1ef60SJohn Polstra  * The "path" argument is a pathname that is used only for error messages.
493124c3e0SJohn Polstra  *
503124c3e0SJohn Polstra  * The return value is a pointer to a newly-allocated Obj_Entry structure
513124c3e0SJohn Polstra  * for the shared object.  Returns NULL on failure.
523124c3e0SJohn Polstra  */
533124c3e0SJohn Polstra Obj_Entry *
547360ae0fSJohn Polstra map_object(int fd, const char *path, const struct stat *sb)
553124c3e0SJohn Polstra {
563124c3e0SJohn Polstra     Obj_Entry *obj;
57341b3de6SMatthew N. Dodd     Elf_Ehdr *hdr;
5878af18bdSDavid E. O'Brien     int i;
5913575fc4SDoug Rabson     Elf_Phdr *phdr;
6013575fc4SDoug Rabson     Elf_Phdr *phlimit;
618b7f25d4SAlexander Kabaev     Elf_Phdr **segs;
623124c3e0SJohn Polstra     int nsegs;
6313575fc4SDoug Rabson     Elf_Phdr *phdyn;
6413575fc4SDoug Rabson     Elf_Phdr *phphdr;
65a607e5d7SJohn Polstra     Elf_Phdr *phinterp;
663124c3e0SJohn Polstra     caddr_t mapbase;
673124c3e0SJohn Polstra     size_t mapsize;
6813575fc4SDoug Rabson     Elf_Off base_offset;
6913575fc4SDoug Rabson     Elf_Addr base_vaddr;
7013575fc4SDoug Rabson     Elf_Addr base_vlimit;
713124c3e0SJohn Polstra     caddr_t base_addr;
7213575fc4SDoug Rabson     Elf_Off data_offset;
7313575fc4SDoug Rabson     Elf_Addr data_vaddr;
7413575fc4SDoug Rabson     Elf_Addr data_vlimit;
753124c3e0SJohn Polstra     caddr_t data_addr;
768b7f25d4SAlexander Kabaev     int data_prot;
77fa7dd9c5SMatthew Dillon     int data_flags;
7813575fc4SDoug Rabson     Elf_Addr clear_vaddr;
793124c3e0SJohn Polstra     caddr_t clear_addr;
808b7f25d4SAlexander Kabaev     caddr_t clear_page;
813124c3e0SJohn Polstra     size_t nclear;
8213575fc4SDoug Rabson     Elf_Addr bss_vaddr;
8313575fc4SDoug Rabson     Elf_Addr bss_vlimit;
843124c3e0SJohn Polstra     caddr_t bss_addr;
853124c3e0SJohn Polstra 
86341b3de6SMatthew N. Dodd     hdr = get_elf_header(fd, path);
87341b3de6SMatthew N. Dodd     if (hdr == NULL)
88341b3de6SMatthew N. Dodd 	return (NULL);
893124c3e0SJohn Polstra 
903124c3e0SJohn Polstra     /*
913124c3e0SJohn Polstra      * Scan the program header entries, and save key information.
923124c3e0SJohn Polstra      *
933124c3e0SJohn Polstra      * We rely on there being exactly two load segments, text and data,
943124c3e0SJohn Polstra      * in that order.
953124c3e0SJohn Polstra      */
96341b3de6SMatthew N. Dodd     phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
97341b3de6SMatthew N. Dodd     phlimit = phdr + hdr->e_phnum;
988b7f25d4SAlexander Kabaev     nsegs = -1;
99a607e5d7SJohn Polstra     phdyn = phphdr = phinterp = NULL;
100341b3de6SMatthew N. Dodd     segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
1013124c3e0SJohn Polstra     while (phdr < phlimit) {
1023124c3e0SJohn Polstra 	switch (phdr->p_type) {
1033124c3e0SJohn Polstra 
104a607e5d7SJohn Polstra 	case PT_INTERP:
105a607e5d7SJohn Polstra 	    phinterp = phdr;
106a607e5d7SJohn Polstra 	    break;
107a607e5d7SJohn Polstra 
1083124c3e0SJohn Polstra 	case PT_LOAD:
1098b7f25d4SAlexander Kabaev 	    segs[++nsegs] = phdr;
1108b7f25d4SAlexander Kabaev     	    if (segs[nsegs]->p_align < PAGE_SIZE) {
1118b7f25d4SAlexander Kabaev 		_rtld_error("%s: PT_LOAD segment %d not page-aligned",
1128b7f25d4SAlexander Kabaev 		    path, nsegs);
113bfb1ef60SJohn Polstra 		return NULL;
114bfb1ef60SJohn Polstra 	    }
1153124c3e0SJohn Polstra 	    break;
1163124c3e0SJohn Polstra 
1173124c3e0SJohn Polstra 	case PT_PHDR:
1183124c3e0SJohn Polstra 	    phphdr = phdr;
1193124c3e0SJohn Polstra 	    break;
1203124c3e0SJohn Polstra 
1213124c3e0SJohn Polstra 	case PT_DYNAMIC:
1223124c3e0SJohn Polstra 	    phdyn = phdr;
1233124c3e0SJohn Polstra 	    break;
1243124c3e0SJohn Polstra 	}
1253124c3e0SJohn Polstra 
1263124c3e0SJohn Polstra 	++phdr;
1273124c3e0SJohn Polstra     }
1283124c3e0SJohn Polstra     if (phdyn == NULL) {
129bfb1ef60SJohn Polstra 	_rtld_error("%s: object is not dynamically-linked", path);
1303124c3e0SJohn Polstra 	return NULL;
1313124c3e0SJohn Polstra     }
1323124c3e0SJohn Polstra 
1338b7f25d4SAlexander Kabaev     if (nsegs < 0) {
134bfb1ef60SJohn Polstra 	_rtld_error("%s: too few PT_LOAD segments", path);
135bfb1ef60SJohn Polstra 	return NULL;
136bfb1ef60SJohn Polstra     }
1373124c3e0SJohn Polstra 
1383124c3e0SJohn Polstra     /*
1393124c3e0SJohn Polstra      * Map the entire address space of the object, to stake out our
1403124c3e0SJohn Polstra      * contiguous region, and to establish the base address for relocation.
1413124c3e0SJohn Polstra      */
1423124c3e0SJohn Polstra     base_offset = trunc_page(segs[0]->p_offset);
1433124c3e0SJohn Polstra     base_vaddr = trunc_page(segs[0]->p_vaddr);
1448b7f25d4SAlexander Kabaev     base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
1453124c3e0SJohn Polstra     mapsize = base_vlimit - base_vaddr;
146341b3de6SMatthew N. Dodd     base_addr = hdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
1473124c3e0SJohn Polstra 
148fa7dd9c5SMatthew Dillon     mapbase = mmap(base_addr, mapsize, convert_prot(segs[0]->p_flags),
149fa7dd9c5SMatthew Dillon       convert_flags(segs[0]->p_flags), fd, base_offset);
1503124c3e0SJohn Polstra     if (mapbase == (caddr_t) -1) {
151bfb1ef60SJohn Polstra 	_rtld_error("%s: mmap of entire address space failed: %s",
152bfb1ef60SJohn Polstra 	  path, strerror(errno));
1533124c3e0SJohn Polstra 	return NULL;
1543124c3e0SJohn Polstra     }
1553124c3e0SJohn Polstra     if (base_addr != NULL && mapbase != base_addr) {
156bfb1ef60SJohn Polstra 	_rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
157bfb1ef60SJohn Polstra 	  path, base_addr, mapbase);
1583124c3e0SJohn Polstra 	munmap(mapbase, mapsize);
1593124c3e0SJohn Polstra 	return NULL;
1603124c3e0SJohn Polstra     }
1613124c3e0SJohn Polstra 
1628b7f25d4SAlexander Kabaev     for (i = 0; i <=  nsegs; i++) {
1638b7f25d4SAlexander Kabaev 	/* Overlay the segment onto the proper region. */
1648b7f25d4SAlexander Kabaev 	data_offset = trunc_page(segs[i]->p_offset);
1658b7f25d4SAlexander Kabaev 	data_vaddr = trunc_page(segs[i]->p_vaddr);
1668b7f25d4SAlexander Kabaev 	data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
1673124c3e0SJohn Polstra 	data_addr = mapbase + (data_vaddr - base_vaddr);
168fa7dd9c5SMatthew Dillon 	data_prot = convert_prot(segs[i]->p_flags);
169fa7dd9c5SMatthew Dillon 	data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
1708b7f25d4SAlexander Kabaev 	/* Do not call mmap on the first segment - this is redundant */
1718b7f25d4SAlexander Kabaev 	if (i && mmap(data_addr, data_vlimit - data_vaddr, data_prot,
172fa7dd9c5SMatthew Dillon 	  data_flags, fd, data_offset) == (caddr_t) -1) {
173bfb1ef60SJohn Polstra 	    _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
1743124c3e0SJohn Polstra 	    return NULL;
1753124c3e0SJohn Polstra 	}
1763124c3e0SJohn Polstra 
1778b7f25d4SAlexander Kabaev 	/* Clear any BSS in the last page of the segment. */
1788b7f25d4SAlexander Kabaev 	clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
1793124c3e0SJohn Polstra 	clear_addr = mapbase + (clear_vaddr - base_vaddr);
1808b7f25d4SAlexander Kabaev 	clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
1818b7f25d4SAlexander Kabaev 	if ((nclear = data_vlimit - clear_vaddr) > 0) {
1828b7f25d4SAlexander Kabaev 	    /* Make sure the end of the segment is writable */
1838b7f25d4SAlexander Kabaev 	    if ((data_prot & PROT_WRITE) == 0 &&
1848b7f25d4SAlexander Kabaev 		-1 ==  mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
1858b7f25d4SAlexander Kabaev 			_rtld_error("%s: mprotect failed: %s", path,
1868b7f25d4SAlexander Kabaev 			    strerror(errno));
1878b7f25d4SAlexander Kabaev 			return NULL;
1888b7f25d4SAlexander Kabaev 	    }
1898b7f25d4SAlexander Kabaev 
1903124c3e0SJohn Polstra 	    memset(clear_addr, 0, nclear);
1913124c3e0SJohn Polstra 
1928b7f25d4SAlexander Kabaev 	    /* Reset the data protection back */
1938b7f25d4SAlexander Kabaev 	    if ((data_prot & PROT_WRITE) == 0)
1948b7f25d4SAlexander Kabaev 		 mprotect(clear_page, PAGE_SIZE, data_prot);
1958b7f25d4SAlexander Kabaev 	}
1968b7f25d4SAlexander Kabaev 
1973124c3e0SJohn Polstra 	/* Overlay the BSS segment onto the proper region. */
1983124c3e0SJohn Polstra 	bss_vaddr = data_vlimit;
1998b7f25d4SAlexander Kabaev 	bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
2003124c3e0SJohn Polstra 	bss_addr = mapbase +  (bss_vaddr - base_vaddr);
2013124c3e0SJohn Polstra 	if (bss_vlimit > bss_vaddr) {	/* There is something to do */
2028b7f25d4SAlexander Kabaev 	    if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
2033124c3e0SJohn Polstra 		MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
2048b7f25d4SAlexander Kabaev 		    _rtld_error("%s: mmap of bss failed: %s", path,
2058b7f25d4SAlexander Kabaev 			strerror(errno));
2063124c3e0SJohn Polstra 		return NULL;
2073124c3e0SJohn Polstra 	    }
2083124c3e0SJohn Polstra 	}
2098b7f25d4SAlexander Kabaev     }
2103124c3e0SJohn Polstra 
211926ea445SJohn Polstra     obj = obj_new();
2127360ae0fSJohn Polstra     if (sb != NULL) {
2137360ae0fSJohn Polstra 	obj->dev = sb->st_dev;
2147360ae0fSJohn Polstra 	obj->ino = sb->st_ino;
2157360ae0fSJohn Polstra     }
2163124c3e0SJohn Polstra     obj->mapbase = mapbase;
2173124c3e0SJohn Polstra     obj->mapsize = mapsize;
2183124c3e0SJohn Polstra     obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
2193124c3e0SJohn Polstra       base_vaddr;
2203124c3e0SJohn Polstra     obj->vaddrbase = base_vaddr;
2213124c3e0SJohn Polstra     obj->relocbase = mapbase - base_vaddr;
222a607e5d7SJohn Polstra     obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
223341b3de6SMatthew N. Dodd     if (hdr->e_entry != 0)
224341b3de6SMatthew N. Dodd 	obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
2253124c3e0SJohn Polstra     if (phphdr != NULL) {
226a607e5d7SJohn Polstra 	obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
2273124c3e0SJohn Polstra 	obj->phsize = phphdr->p_memsz;
2283124c3e0SJohn Polstra     }
229a607e5d7SJohn Polstra     if (phinterp != NULL)
230a607e5d7SJohn Polstra 	obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
2313124c3e0SJohn Polstra 
2323124c3e0SJohn Polstra     return obj;
2333124c3e0SJohn Polstra }
2343124c3e0SJohn Polstra 
235341b3de6SMatthew N. Dodd static Elf_Ehdr *
236341b3de6SMatthew N. Dodd get_elf_header (int fd, const char *path)
237341b3de6SMatthew N. Dodd {
238341b3de6SMatthew N. Dodd     static union {
239341b3de6SMatthew N. Dodd 	Elf_Ehdr hdr;
240341b3de6SMatthew N. Dodd 	char buf[PAGE_SIZE];
241341b3de6SMatthew N. Dodd     } u;
242341b3de6SMatthew N. Dodd     ssize_t nbytes;
243341b3de6SMatthew N. Dodd 
244341b3de6SMatthew N. Dodd     if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
245341b3de6SMatthew N. Dodd 	_rtld_error("%s: read error: %s", path, strerror(errno));
246341b3de6SMatthew N. Dodd 	return NULL;
247341b3de6SMatthew N. Dodd     }
248341b3de6SMatthew N. Dodd 
249341b3de6SMatthew N. Dodd     /* Make sure the file is valid */
250341b3de6SMatthew N. Dodd     if (nbytes < (ssize_t)sizeof(Elf_Ehdr) || !IS_ELF(u.hdr)) {
251341b3de6SMatthew N. Dodd 	_rtld_error("%s: invalid file format", path);
252341b3de6SMatthew N. Dodd 	return NULL;
253341b3de6SMatthew N. Dodd     }
254341b3de6SMatthew N. Dodd     if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
255341b3de6SMatthew N. Dodd       || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
256341b3de6SMatthew N. Dodd 	_rtld_error("%s: unsupported file layout", path);
257341b3de6SMatthew N. Dodd 	return NULL;
258341b3de6SMatthew N. Dodd     }
259341b3de6SMatthew N. Dodd     if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
260341b3de6SMatthew N. Dodd       || u.hdr.e_version != EV_CURRENT) {
261341b3de6SMatthew N. Dodd 	_rtld_error("%s: unsupported file version", path);
262341b3de6SMatthew N. Dodd 	return NULL;
263341b3de6SMatthew N. Dodd     }
264341b3de6SMatthew N. Dodd     if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
265341b3de6SMatthew N. Dodd 	_rtld_error("%s: unsupported file type", path);
266341b3de6SMatthew N. Dodd 	return NULL;
267341b3de6SMatthew N. Dodd     }
268341b3de6SMatthew N. Dodd     if (u.hdr.e_machine != ELF_TARG_MACH) {
269341b3de6SMatthew N. Dodd 	_rtld_error("%s: unsupported machine", path);
270341b3de6SMatthew N. Dodd 	return NULL;
271341b3de6SMatthew N. Dodd     }
272341b3de6SMatthew N. Dodd 
273341b3de6SMatthew N. Dodd     /*
274341b3de6SMatthew N. Dodd      * We rely on the program header being in the first page.  This is
275341b3de6SMatthew N. Dodd      * not strictly required by the ABI specification, but it seems to
276341b3de6SMatthew N. Dodd      * always true in practice.  And, it simplifies things considerably.
277341b3de6SMatthew N. Dodd      */
278341b3de6SMatthew N. Dodd     if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
279341b3de6SMatthew N. Dodd 	_rtld_error(
280341b3de6SMatthew N. Dodd 	  "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
281341b3de6SMatthew N. Dodd 	return NULL;
282341b3de6SMatthew N. Dodd     }
283341b3de6SMatthew N. Dodd     if (u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) > (size_t)nbytes) {
284341b3de6SMatthew N. Dodd 	_rtld_error("%s: program header too large", path);
285341b3de6SMatthew N. Dodd 	return NULL;
286341b3de6SMatthew N. Dodd     }
287341b3de6SMatthew N. Dodd 
288341b3de6SMatthew N. Dodd     return (&u.hdr);
289341b3de6SMatthew N. Dodd }
290341b3de6SMatthew N. Dodd 
291926ea445SJohn Polstra void
292926ea445SJohn Polstra obj_free(Obj_Entry *obj)
293926ea445SJohn Polstra {
294926ea445SJohn Polstra     Objlist_Entry *elm;
295926ea445SJohn Polstra 
296926ea445SJohn Polstra     free(obj->path);
297926ea445SJohn Polstra     while (obj->needed != NULL) {
298926ea445SJohn Polstra 	Needed_Entry *needed = obj->needed;
299926ea445SJohn Polstra 	obj->needed = needed->next;
300926ea445SJohn Polstra 	free(needed);
301926ea445SJohn Polstra     }
302926ea445SJohn Polstra     while (!STAILQ_EMPTY(&obj->dldags)) {
303926ea445SJohn Polstra 	elm = STAILQ_FIRST(&obj->dldags);
304926ea445SJohn Polstra 	STAILQ_REMOVE_HEAD(&obj->dldags, link);
305926ea445SJohn Polstra 	free(elm);
306926ea445SJohn Polstra     }
307926ea445SJohn Polstra     while (!STAILQ_EMPTY(&obj->dagmembers)) {
308926ea445SJohn Polstra 	elm = STAILQ_FIRST(&obj->dagmembers);
309926ea445SJohn Polstra 	STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
310926ea445SJohn Polstra 	free(elm);
311926ea445SJohn Polstra     }
312da9f2454SMatthew N. Dodd     free(obj->origin_path);
31363c1e7cbSAlexander Kabaev     free(obj->priv);
314926ea445SJohn Polstra     free(obj);
315926ea445SJohn Polstra }
316926ea445SJohn Polstra 
317926ea445SJohn Polstra Obj_Entry *
318926ea445SJohn Polstra obj_new(void)
319926ea445SJohn Polstra {
320926ea445SJohn Polstra     Obj_Entry *obj;
321926ea445SJohn Polstra 
322926ea445SJohn Polstra     obj = CNEW(Obj_Entry);
323926ea445SJohn Polstra     STAILQ_INIT(&obj->dldags);
324926ea445SJohn Polstra     STAILQ_INIT(&obj->dagmembers);
325926ea445SJohn Polstra     return obj;
326926ea445SJohn Polstra }
327926ea445SJohn Polstra 
3283124c3e0SJohn Polstra /*
3293124c3e0SJohn Polstra  * Given a set of ELF protection flags, return the corresponding protection
3303124c3e0SJohn Polstra  * flags for MMAP.
3313124c3e0SJohn Polstra  */
3323124c3e0SJohn Polstra static int
333fa7dd9c5SMatthew Dillon convert_prot(int elfflags)
3343124c3e0SJohn Polstra {
3353124c3e0SJohn Polstra     int prot = 0;
3363124c3e0SJohn Polstra     if (elfflags & PF_R)
3373124c3e0SJohn Polstra 	prot |= PROT_READ;
3383124c3e0SJohn Polstra     if (elfflags & PF_W)
3393124c3e0SJohn Polstra 	prot |= PROT_WRITE;
3403124c3e0SJohn Polstra     if (elfflags & PF_X)
3413124c3e0SJohn Polstra 	prot |= PROT_EXEC;
3423124c3e0SJohn Polstra     return prot;
3433124c3e0SJohn Polstra }
344fa7dd9c5SMatthew Dillon 
345fa7dd9c5SMatthew Dillon static int
346fa7dd9c5SMatthew Dillon convert_flags(int elfflags)
347fa7dd9c5SMatthew Dillon {
348fa7dd9c5SMatthew Dillon     int flags = MAP_PRIVATE; /* All mappings are private */
349fa7dd9c5SMatthew Dillon 
350fa7dd9c5SMatthew Dillon     /*
351fa7dd9c5SMatthew Dillon      * Readonly mappings are marked "MAP_NOCORE", because they can be
352fa7dd9c5SMatthew Dillon      * reconstructed by a debugger.
353fa7dd9c5SMatthew Dillon      */
354fa7dd9c5SMatthew Dillon     if (!(elfflags & PF_W))
355fa7dd9c5SMatthew Dillon 	flags |= MAP_NOCORE;
356fa7dd9c5SMatthew Dillon     return flags;
357fa7dd9c5SMatthew Dillon }
358