xref: /freebsd/libexec/rtld-elf/map_object.c (revision 017246d02f18aa1b754027fe6c058a76659e66b7)
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;
66017246d0SDoug Rabson     Elf_Phdr *phtls;
673124c3e0SJohn Polstra     caddr_t mapbase;
683124c3e0SJohn Polstra     size_t mapsize;
6913575fc4SDoug Rabson     Elf_Off base_offset;
7013575fc4SDoug Rabson     Elf_Addr base_vaddr;
7113575fc4SDoug Rabson     Elf_Addr base_vlimit;
723124c3e0SJohn Polstra     caddr_t base_addr;
7313575fc4SDoug Rabson     Elf_Off data_offset;
7413575fc4SDoug Rabson     Elf_Addr data_vaddr;
7513575fc4SDoug Rabson     Elf_Addr data_vlimit;
763124c3e0SJohn Polstra     caddr_t data_addr;
778b7f25d4SAlexander Kabaev     int data_prot;
78fa7dd9c5SMatthew Dillon     int data_flags;
7913575fc4SDoug Rabson     Elf_Addr clear_vaddr;
803124c3e0SJohn Polstra     caddr_t clear_addr;
818b7f25d4SAlexander Kabaev     caddr_t clear_page;
823124c3e0SJohn Polstra     size_t nclear;
8313575fc4SDoug Rabson     Elf_Addr bss_vaddr;
8413575fc4SDoug Rabson     Elf_Addr bss_vlimit;
853124c3e0SJohn Polstra     caddr_t bss_addr;
863124c3e0SJohn Polstra 
87341b3de6SMatthew N. Dodd     hdr = get_elf_header(fd, path);
88341b3de6SMatthew N. Dodd     if (hdr == NULL)
89341b3de6SMatthew N. Dodd 	return (NULL);
903124c3e0SJohn Polstra 
913124c3e0SJohn Polstra     /*
923124c3e0SJohn Polstra      * Scan the program header entries, and save key information.
933124c3e0SJohn Polstra      *
943124c3e0SJohn Polstra      * We rely on there being exactly two load segments, text and data,
953124c3e0SJohn Polstra      * in that order.
963124c3e0SJohn Polstra      */
97341b3de6SMatthew N. Dodd     phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
98341b3de6SMatthew N. Dodd     phlimit = phdr + hdr->e_phnum;
998b7f25d4SAlexander Kabaev     nsegs = -1;
100017246d0SDoug Rabson     phdyn = phphdr = phinterp = phtls = NULL;
101341b3de6SMatthew N. Dodd     segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
1023124c3e0SJohn Polstra     while (phdr < phlimit) {
1033124c3e0SJohn Polstra 	switch (phdr->p_type) {
1043124c3e0SJohn Polstra 
105a607e5d7SJohn Polstra 	case PT_INTERP:
106a607e5d7SJohn Polstra 	    phinterp = phdr;
107a607e5d7SJohn Polstra 	    break;
108a607e5d7SJohn Polstra 
1093124c3e0SJohn Polstra 	case PT_LOAD:
1108b7f25d4SAlexander Kabaev 	    segs[++nsegs] = phdr;
1118b7f25d4SAlexander Kabaev     	    if (segs[nsegs]->p_align < PAGE_SIZE) {
1128b7f25d4SAlexander Kabaev 		_rtld_error("%s: PT_LOAD segment %d not page-aligned",
1138b7f25d4SAlexander Kabaev 		    path, nsegs);
114bfb1ef60SJohn Polstra 		return NULL;
115bfb1ef60SJohn Polstra 	    }
1163124c3e0SJohn Polstra 	    break;
1173124c3e0SJohn Polstra 
1183124c3e0SJohn Polstra 	case PT_PHDR:
1193124c3e0SJohn Polstra 	    phphdr = phdr;
1203124c3e0SJohn Polstra 	    break;
1213124c3e0SJohn Polstra 
1223124c3e0SJohn Polstra 	case PT_DYNAMIC:
1233124c3e0SJohn Polstra 	    phdyn = phdr;
1243124c3e0SJohn Polstra 	    break;
125017246d0SDoug Rabson 
126017246d0SDoug Rabson 	case PT_TLS:
127017246d0SDoug Rabson 	    phtls = phdr;
128017246d0SDoug Rabson 	    break;
1293124c3e0SJohn Polstra 	}
1303124c3e0SJohn Polstra 
1313124c3e0SJohn Polstra 	++phdr;
1323124c3e0SJohn Polstra     }
1333124c3e0SJohn Polstra     if (phdyn == NULL) {
134bfb1ef60SJohn Polstra 	_rtld_error("%s: object is not dynamically-linked", path);
1353124c3e0SJohn Polstra 	return NULL;
1363124c3e0SJohn Polstra     }
1373124c3e0SJohn Polstra 
1388b7f25d4SAlexander Kabaev     if (nsegs < 0) {
139bfb1ef60SJohn Polstra 	_rtld_error("%s: too few PT_LOAD segments", path);
140bfb1ef60SJohn Polstra 	return NULL;
141bfb1ef60SJohn Polstra     }
1423124c3e0SJohn Polstra 
1433124c3e0SJohn Polstra     /*
1443124c3e0SJohn Polstra      * Map the entire address space of the object, to stake out our
1453124c3e0SJohn Polstra      * contiguous region, and to establish the base address for relocation.
1463124c3e0SJohn Polstra      */
1473124c3e0SJohn Polstra     base_offset = trunc_page(segs[0]->p_offset);
1483124c3e0SJohn Polstra     base_vaddr = trunc_page(segs[0]->p_vaddr);
1498b7f25d4SAlexander Kabaev     base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
1503124c3e0SJohn Polstra     mapsize = base_vlimit - base_vaddr;
151341b3de6SMatthew N. Dodd     base_addr = hdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
1523124c3e0SJohn Polstra 
153fa7dd9c5SMatthew Dillon     mapbase = mmap(base_addr, mapsize, convert_prot(segs[0]->p_flags),
154fa7dd9c5SMatthew Dillon       convert_flags(segs[0]->p_flags), fd, base_offset);
1553124c3e0SJohn Polstra     if (mapbase == (caddr_t) -1) {
156bfb1ef60SJohn Polstra 	_rtld_error("%s: mmap of entire address space failed: %s",
157bfb1ef60SJohn Polstra 	  path, strerror(errno));
1583124c3e0SJohn Polstra 	return NULL;
1593124c3e0SJohn Polstra     }
1603124c3e0SJohn Polstra     if (base_addr != NULL && mapbase != base_addr) {
161bfb1ef60SJohn Polstra 	_rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
162bfb1ef60SJohn Polstra 	  path, base_addr, mapbase);
1633124c3e0SJohn Polstra 	munmap(mapbase, mapsize);
1643124c3e0SJohn Polstra 	return NULL;
1653124c3e0SJohn Polstra     }
1663124c3e0SJohn Polstra 
1678b7f25d4SAlexander Kabaev     for (i = 0; i <=  nsegs; i++) {
1688b7f25d4SAlexander Kabaev 	/* Overlay the segment onto the proper region. */
1698b7f25d4SAlexander Kabaev 	data_offset = trunc_page(segs[i]->p_offset);
1708b7f25d4SAlexander Kabaev 	data_vaddr = trunc_page(segs[i]->p_vaddr);
1718b7f25d4SAlexander Kabaev 	data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
1723124c3e0SJohn Polstra 	data_addr = mapbase + (data_vaddr - base_vaddr);
173fa7dd9c5SMatthew Dillon 	data_prot = convert_prot(segs[i]->p_flags);
174fa7dd9c5SMatthew Dillon 	data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
1758b7f25d4SAlexander Kabaev 	/* Do not call mmap on the first segment - this is redundant */
1768b7f25d4SAlexander Kabaev 	if (i && mmap(data_addr, data_vlimit - data_vaddr, data_prot,
177fa7dd9c5SMatthew Dillon 	  data_flags, fd, data_offset) == (caddr_t) -1) {
178bfb1ef60SJohn Polstra 	    _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
1793124c3e0SJohn Polstra 	    return NULL;
1803124c3e0SJohn Polstra 	}
1813124c3e0SJohn Polstra 
1828b7f25d4SAlexander Kabaev 	/* Clear any BSS in the last page of the segment. */
1838b7f25d4SAlexander Kabaev 	clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
1843124c3e0SJohn Polstra 	clear_addr = mapbase + (clear_vaddr - base_vaddr);
1858b7f25d4SAlexander Kabaev 	clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
1868b7f25d4SAlexander Kabaev 	if ((nclear = data_vlimit - clear_vaddr) > 0) {
1878b7f25d4SAlexander Kabaev 	    /* Make sure the end of the segment is writable */
1888b7f25d4SAlexander Kabaev 	    if ((data_prot & PROT_WRITE) == 0 &&
1898b7f25d4SAlexander Kabaev 		-1 ==  mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
1908b7f25d4SAlexander Kabaev 			_rtld_error("%s: mprotect failed: %s", path,
1918b7f25d4SAlexander Kabaev 			    strerror(errno));
1928b7f25d4SAlexander Kabaev 			return NULL;
1938b7f25d4SAlexander Kabaev 	    }
1948b7f25d4SAlexander Kabaev 
1953124c3e0SJohn Polstra 	    memset(clear_addr, 0, nclear);
1963124c3e0SJohn Polstra 
1978b7f25d4SAlexander Kabaev 	    /* Reset the data protection back */
1988b7f25d4SAlexander Kabaev 	    if ((data_prot & PROT_WRITE) == 0)
1998b7f25d4SAlexander Kabaev 		 mprotect(clear_page, PAGE_SIZE, data_prot);
2008b7f25d4SAlexander Kabaev 	}
2018b7f25d4SAlexander Kabaev 
2023124c3e0SJohn Polstra 	/* Overlay the BSS segment onto the proper region. */
2033124c3e0SJohn Polstra 	bss_vaddr = data_vlimit;
2048b7f25d4SAlexander Kabaev 	bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
2053124c3e0SJohn Polstra 	bss_addr = mapbase +  (bss_vaddr - base_vaddr);
2063124c3e0SJohn Polstra 	if (bss_vlimit > bss_vaddr) {	/* There is something to do */
2078b7f25d4SAlexander Kabaev 	    if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
2083124c3e0SJohn Polstra 		MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
2098b7f25d4SAlexander Kabaev 		    _rtld_error("%s: mmap of bss failed: %s", path,
2108b7f25d4SAlexander Kabaev 			strerror(errno));
2113124c3e0SJohn Polstra 		return NULL;
2123124c3e0SJohn Polstra 	    }
2133124c3e0SJohn Polstra 	}
2148b7f25d4SAlexander Kabaev     }
2153124c3e0SJohn Polstra 
216926ea445SJohn Polstra     obj = obj_new();
2177360ae0fSJohn Polstra     if (sb != NULL) {
2187360ae0fSJohn Polstra 	obj->dev = sb->st_dev;
2197360ae0fSJohn Polstra 	obj->ino = sb->st_ino;
2207360ae0fSJohn Polstra     }
2213124c3e0SJohn Polstra     obj->mapbase = mapbase;
2223124c3e0SJohn Polstra     obj->mapsize = mapsize;
2233124c3e0SJohn Polstra     obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
2243124c3e0SJohn Polstra       base_vaddr;
2253124c3e0SJohn Polstra     obj->vaddrbase = base_vaddr;
2263124c3e0SJohn Polstra     obj->relocbase = mapbase - base_vaddr;
227a607e5d7SJohn Polstra     obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
228341b3de6SMatthew N. Dodd     if (hdr->e_entry != 0)
229341b3de6SMatthew N. Dodd 	obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
2303124c3e0SJohn Polstra     if (phphdr != NULL) {
231a607e5d7SJohn Polstra 	obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
2323124c3e0SJohn Polstra 	obj->phsize = phphdr->p_memsz;
2333124c3e0SJohn Polstra     }
234a607e5d7SJohn Polstra     if (phinterp != NULL)
235a607e5d7SJohn Polstra 	obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
236017246d0SDoug Rabson     if (phtls != NULL) {
237017246d0SDoug Rabson 	tls_dtv_generation++;
238017246d0SDoug Rabson 	obj->tlsindex = ++tls_max_index;
239017246d0SDoug Rabson 	obj->tlssize = phtls->p_memsz;
240017246d0SDoug Rabson 	obj->tlsalign = phtls->p_align;
241017246d0SDoug Rabson 	obj->tlsinitsize = phtls->p_filesz;
242017246d0SDoug Rabson 	obj->tlsinit = mapbase + phtls->p_vaddr;
243017246d0SDoug Rabson     }
2443124c3e0SJohn Polstra     return obj;
2453124c3e0SJohn Polstra }
2463124c3e0SJohn Polstra 
247341b3de6SMatthew N. Dodd static Elf_Ehdr *
248341b3de6SMatthew N. Dodd get_elf_header (int fd, const char *path)
249341b3de6SMatthew N. Dodd {
250341b3de6SMatthew N. Dodd     static union {
251341b3de6SMatthew N. Dodd 	Elf_Ehdr hdr;
252341b3de6SMatthew N. Dodd 	char buf[PAGE_SIZE];
253341b3de6SMatthew N. Dodd     } u;
254341b3de6SMatthew N. Dodd     ssize_t nbytes;
255341b3de6SMatthew N. Dodd 
256341b3de6SMatthew N. Dodd     if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
257341b3de6SMatthew N. Dodd 	_rtld_error("%s: read error: %s", path, strerror(errno));
258341b3de6SMatthew N. Dodd 	return NULL;
259341b3de6SMatthew N. Dodd     }
260341b3de6SMatthew N. Dodd 
261341b3de6SMatthew N. Dodd     /* Make sure the file is valid */
262341b3de6SMatthew N. Dodd     if (nbytes < (ssize_t)sizeof(Elf_Ehdr) || !IS_ELF(u.hdr)) {
263341b3de6SMatthew N. Dodd 	_rtld_error("%s: invalid file format", path);
264341b3de6SMatthew N. Dodd 	return NULL;
265341b3de6SMatthew N. Dodd     }
266341b3de6SMatthew N. Dodd     if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
267341b3de6SMatthew N. Dodd       || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
268341b3de6SMatthew N. Dodd 	_rtld_error("%s: unsupported file layout", path);
269341b3de6SMatthew N. Dodd 	return NULL;
270341b3de6SMatthew N. Dodd     }
271341b3de6SMatthew N. Dodd     if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
272341b3de6SMatthew N. Dodd       || u.hdr.e_version != EV_CURRENT) {
273341b3de6SMatthew N. Dodd 	_rtld_error("%s: unsupported file version", path);
274341b3de6SMatthew N. Dodd 	return NULL;
275341b3de6SMatthew N. Dodd     }
276341b3de6SMatthew N. Dodd     if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
277341b3de6SMatthew N. Dodd 	_rtld_error("%s: unsupported file type", path);
278341b3de6SMatthew N. Dodd 	return NULL;
279341b3de6SMatthew N. Dodd     }
280341b3de6SMatthew N. Dodd     if (u.hdr.e_machine != ELF_TARG_MACH) {
281341b3de6SMatthew N. Dodd 	_rtld_error("%s: unsupported machine", path);
282341b3de6SMatthew N. Dodd 	return NULL;
283341b3de6SMatthew N. Dodd     }
284341b3de6SMatthew N. Dodd 
285341b3de6SMatthew N. Dodd     /*
286341b3de6SMatthew N. Dodd      * We rely on the program header being in the first page.  This is
287341b3de6SMatthew N. Dodd      * not strictly required by the ABI specification, but it seems to
288341b3de6SMatthew N. Dodd      * always true in practice.  And, it simplifies things considerably.
289341b3de6SMatthew N. Dodd      */
290341b3de6SMatthew N. Dodd     if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
291341b3de6SMatthew N. Dodd 	_rtld_error(
292341b3de6SMatthew N. Dodd 	  "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
293341b3de6SMatthew N. Dodd 	return NULL;
294341b3de6SMatthew N. Dodd     }
295341b3de6SMatthew N. Dodd     if (u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) > (size_t)nbytes) {
296341b3de6SMatthew N. Dodd 	_rtld_error("%s: program header too large", path);
297341b3de6SMatthew N. Dodd 	return NULL;
298341b3de6SMatthew N. Dodd     }
299341b3de6SMatthew N. Dodd 
300341b3de6SMatthew N. Dodd     return (&u.hdr);
301341b3de6SMatthew N. Dodd }
302341b3de6SMatthew N. Dodd 
303926ea445SJohn Polstra void
304926ea445SJohn Polstra obj_free(Obj_Entry *obj)
305926ea445SJohn Polstra {
306926ea445SJohn Polstra     Objlist_Entry *elm;
307926ea445SJohn Polstra 
308926ea445SJohn Polstra     free(obj->path);
309926ea445SJohn Polstra     while (obj->needed != NULL) {
310926ea445SJohn Polstra 	Needed_Entry *needed = obj->needed;
311926ea445SJohn Polstra 	obj->needed = needed->next;
312926ea445SJohn Polstra 	free(needed);
313926ea445SJohn Polstra     }
314926ea445SJohn Polstra     while (!STAILQ_EMPTY(&obj->dldags)) {
315926ea445SJohn Polstra 	elm = STAILQ_FIRST(&obj->dldags);
316926ea445SJohn Polstra 	STAILQ_REMOVE_HEAD(&obj->dldags, link);
317926ea445SJohn Polstra 	free(elm);
318926ea445SJohn Polstra     }
319926ea445SJohn Polstra     while (!STAILQ_EMPTY(&obj->dagmembers)) {
320926ea445SJohn Polstra 	elm = STAILQ_FIRST(&obj->dagmembers);
321926ea445SJohn Polstra 	STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
322926ea445SJohn Polstra 	free(elm);
323926ea445SJohn Polstra     }
324da9f2454SMatthew N. Dodd     free(obj->origin_path);
32563c1e7cbSAlexander Kabaev     free(obj->priv);
326926ea445SJohn Polstra     free(obj);
327926ea445SJohn Polstra }
328926ea445SJohn Polstra 
329926ea445SJohn Polstra Obj_Entry *
330926ea445SJohn Polstra obj_new(void)
331926ea445SJohn Polstra {
332926ea445SJohn Polstra     Obj_Entry *obj;
333926ea445SJohn Polstra 
334926ea445SJohn Polstra     obj = CNEW(Obj_Entry);
335926ea445SJohn Polstra     STAILQ_INIT(&obj->dldags);
336926ea445SJohn Polstra     STAILQ_INIT(&obj->dagmembers);
337926ea445SJohn Polstra     return obj;
338926ea445SJohn Polstra }
339926ea445SJohn Polstra 
3403124c3e0SJohn Polstra /*
3413124c3e0SJohn Polstra  * Given a set of ELF protection flags, return the corresponding protection
3423124c3e0SJohn Polstra  * flags for MMAP.
3433124c3e0SJohn Polstra  */
3443124c3e0SJohn Polstra static int
345fa7dd9c5SMatthew Dillon convert_prot(int elfflags)
3463124c3e0SJohn Polstra {
3473124c3e0SJohn Polstra     int prot = 0;
3483124c3e0SJohn Polstra     if (elfflags & PF_R)
3493124c3e0SJohn Polstra 	prot |= PROT_READ;
3503124c3e0SJohn Polstra     if (elfflags & PF_W)
3513124c3e0SJohn Polstra 	prot |= PROT_WRITE;
3523124c3e0SJohn Polstra     if (elfflags & PF_X)
3533124c3e0SJohn Polstra 	prot |= PROT_EXEC;
3543124c3e0SJohn Polstra     return prot;
3553124c3e0SJohn Polstra }
356fa7dd9c5SMatthew Dillon 
357fa7dd9c5SMatthew Dillon static int
358fa7dd9c5SMatthew Dillon convert_flags(int elfflags)
359fa7dd9c5SMatthew Dillon {
360fa7dd9c5SMatthew Dillon     int flags = MAP_PRIVATE; /* All mappings are private */
361fa7dd9c5SMatthew Dillon 
362fa7dd9c5SMatthew Dillon     /*
363fa7dd9c5SMatthew Dillon      * Readonly mappings are marked "MAP_NOCORE", because they can be
364fa7dd9c5SMatthew Dillon      * reconstructed by a debugger.
365fa7dd9c5SMatthew Dillon      */
366fa7dd9c5SMatthew Dillon     if (!(elfflags & PF_W))
367fa7dd9c5SMatthew Dillon 	flags |= MAP_NOCORE;
368fa7dd9c5SMatthew Dillon     return flags;
369fa7dd9c5SMatthew Dillon }
370