xref: /freebsd/libexec/rtld-elf/map_object.c (revision fa7dd9c5bc8e36e355cfe61a87e27779f539eef1)
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 
41fa7dd9c5SMatthew Dillon static int convert_prot(int);	/* Elf flags -> mmap protection */
42fa7dd9c5SMatthew Dillon static int convert_flags(int); /* Elf flags -> mmap flags */
433124c3e0SJohn Polstra 
443124c3e0SJohn Polstra /*
45bfb1ef60SJohn Polstra  * Map a shared object into memory.  The "fd" argument is a file descriptor,
463124c3e0SJohn Polstra  * which must be open on the object and positioned at its beginning.
47bfb1ef60SJohn Polstra  * The "path" argument is a pathname that is used only for error messages.
483124c3e0SJohn Polstra  *
493124c3e0SJohn Polstra  * The return value is a pointer to a newly-allocated Obj_Entry structure
503124c3e0SJohn Polstra  * for the shared object.  Returns NULL on failure.
513124c3e0SJohn Polstra  */
523124c3e0SJohn Polstra Obj_Entry *
537360ae0fSJohn Polstra map_object(int fd, const char *path, const struct stat *sb)
543124c3e0SJohn Polstra {
553124c3e0SJohn Polstra     Obj_Entry *obj;
563124c3e0SJohn Polstra     union {
5713575fc4SDoug Rabson 	Elf_Ehdr hdr;
583124c3e0SJohn Polstra 	char buf[PAGE_SIZE];
593124c3e0SJohn Polstra     } u;
608b7f25d4SAlexander Kabaev     int nbytes, i;
6113575fc4SDoug Rabson     Elf_Phdr *phdr;
6213575fc4SDoug Rabson     Elf_Phdr *phlimit;
638b7f25d4SAlexander Kabaev     Elf_Phdr **segs;
643124c3e0SJohn Polstra     int nsegs;
6513575fc4SDoug Rabson     Elf_Phdr *phdyn;
6613575fc4SDoug Rabson     Elf_Phdr *phphdr;
67a607e5d7SJohn Polstra     Elf_Phdr *phinterp;
683124c3e0SJohn Polstra     caddr_t mapbase;
693124c3e0SJohn Polstra     size_t mapsize;
7013575fc4SDoug Rabson     Elf_Off base_offset;
7113575fc4SDoug Rabson     Elf_Addr base_vaddr;
7213575fc4SDoug Rabson     Elf_Addr base_vlimit;
733124c3e0SJohn Polstra     caddr_t base_addr;
7413575fc4SDoug Rabson     Elf_Off data_offset;
7513575fc4SDoug Rabson     Elf_Addr data_vaddr;
7613575fc4SDoug Rabson     Elf_Addr data_vlimit;
773124c3e0SJohn Polstra     caddr_t data_addr;
788b7f25d4SAlexander Kabaev     int data_prot;
79fa7dd9c5SMatthew Dillon     int data_flags;
8013575fc4SDoug Rabson     Elf_Addr clear_vaddr;
813124c3e0SJohn Polstra     caddr_t clear_addr;
828b7f25d4SAlexander Kabaev     caddr_t clear_page;
833124c3e0SJohn Polstra     size_t nclear;
8413575fc4SDoug Rabson     Elf_Addr bss_vaddr;
8513575fc4SDoug Rabson     Elf_Addr bss_vlimit;
863124c3e0SJohn Polstra     caddr_t bss_addr;
873124c3e0SJohn Polstra 
883124c3e0SJohn Polstra     if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
89bfb1ef60SJohn Polstra 	_rtld_error("%s: read error: %s", path, strerror(errno));
903124c3e0SJohn Polstra 	return NULL;
913124c3e0SJohn Polstra     }
923124c3e0SJohn Polstra 
933124c3e0SJohn Polstra     /* Make sure the file is valid */
9413575fc4SDoug Rabson     if (nbytes < sizeof(Elf_Ehdr)
953124c3e0SJohn Polstra       || u.hdr.e_ident[EI_MAG0] != ELFMAG0
963124c3e0SJohn Polstra       || u.hdr.e_ident[EI_MAG1] != ELFMAG1
973124c3e0SJohn Polstra       || u.hdr.e_ident[EI_MAG2] != ELFMAG2
983124c3e0SJohn Polstra       || u.hdr.e_ident[EI_MAG3] != ELFMAG3) {
99bfb1ef60SJohn Polstra 	_rtld_error("%s: invalid file format", path);
1003124c3e0SJohn Polstra 	return NULL;
1013124c3e0SJohn Polstra     }
10213575fc4SDoug Rabson     if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
10313575fc4SDoug Rabson       || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
104bfb1ef60SJohn Polstra 	_rtld_error("%s: unsupported file layout", path);
1053124c3e0SJohn Polstra 	return NULL;
1063124c3e0SJohn Polstra     }
1073124c3e0SJohn Polstra     if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
1083124c3e0SJohn Polstra       || u.hdr.e_version != EV_CURRENT) {
109bfb1ef60SJohn Polstra 	_rtld_error("%s: unsupported file version", path);
1103124c3e0SJohn Polstra 	return NULL;
1113124c3e0SJohn Polstra     }
1123124c3e0SJohn Polstra     if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
113bfb1ef60SJohn Polstra 	_rtld_error("%s: unsupported file type", path);
1143124c3e0SJohn Polstra 	return NULL;
1153124c3e0SJohn Polstra     }
11613575fc4SDoug Rabson     if (u.hdr.e_machine != ELF_TARG_MACH) {
117bfb1ef60SJohn Polstra 	_rtld_error("%s: unsupported machine", path);
1183124c3e0SJohn Polstra 	return NULL;
1193124c3e0SJohn Polstra     }
1203124c3e0SJohn Polstra 
1213124c3e0SJohn Polstra     /*
1223124c3e0SJohn Polstra      * We rely on the program header being in the first page.  This is
1233124c3e0SJohn Polstra      * not strictly required by the ABI specification, but it seems to
1243124c3e0SJohn Polstra      * always true in practice.  And, it simplifies things considerably.
1253124c3e0SJohn Polstra      */
126bfb1ef60SJohn Polstra     if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
127bfb1ef60SJohn Polstra 	_rtld_error(
128bfb1ef60SJohn Polstra 	  "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
129bfb1ef60SJohn Polstra 	return NULL;
130bfb1ef60SJohn Polstra     }
131bfb1ef60SJohn Polstra     if (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) > nbytes) {
132bfb1ef60SJohn Polstra 	_rtld_error("%s: program header too large", path);
133bfb1ef60SJohn Polstra 	return NULL;
134bfb1ef60SJohn Polstra     }
1353124c3e0SJohn Polstra 
1363124c3e0SJohn Polstra     /*
1373124c3e0SJohn Polstra      * Scan the program header entries, and save key information.
1383124c3e0SJohn Polstra      *
1393124c3e0SJohn Polstra      * We rely on there being exactly two load segments, text and data,
1403124c3e0SJohn Polstra      * in that order.
1413124c3e0SJohn Polstra      */
14213575fc4SDoug Rabson     phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff);
1433124c3e0SJohn Polstra     phlimit = phdr + u.hdr.e_phnum;
1448b7f25d4SAlexander Kabaev     nsegs = -1;
145a607e5d7SJohn Polstra     phdyn = phphdr = phinterp = NULL;
1468b7f25d4SAlexander Kabaev     segs = alloca(sizeof(segs[0]) * u.hdr.e_phnum);
1473124c3e0SJohn Polstra     while (phdr < phlimit) {
1483124c3e0SJohn Polstra 	switch (phdr->p_type) {
1493124c3e0SJohn Polstra 
150a607e5d7SJohn Polstra 	case PT_INTERP:
151a607e5d7SJohn Polstra 	    phinterp = phdr;
152a607e5d7SJohn Polstra 	    break;
153a607e5d7SJohn Polstra 
1543124c3e0SJohn Polstra 	case PT_LOAD:
1558b7f25d4SAlexander Kabaev 	    segs[++nsegs] = phdr;
1568b7f25d4SAlexander Kabaev     	    if (segs[nsegs]->p_align < PAGE_SIZE) {
1578b7f25d4SAlexander Kabaev 		_rtld_error("%s: PT_LOAD segment %d not page-aligned",
1588b7f25d4SAlexander Kabaev 		    path, nsegs);
159bfb1ef60SJohn Polstra 		return NULL;
160bfb1ef60SJohn Polstra 	    }
1613124c3e0SJohn Polstra 	    break;
1623124c3e0SJohn Polstra 
1633124c3e0SJohn Polstra 	case PT_PHDR:
1643124c3e0SJohn Polstra 	    phphdr = phdr;
1653124c3e0SJohn Polstra 	    break;
1663124c3e0SJohn Polstra 
1673124c3e0SJohn Polstra 	case PT_DYNAMIC:
1683124c3e0SJohn Polstra 	    phdyn = phdr;
1693124c3e0SJohn Polstra 	    break;
1703124c3e0SJohn Polstra 	}
1713124c3e0SJohn Polstra 
1723124c3e0SJohn Polstra 	++phdr;
1733124c3e0SJohn Polstra     }
1743124c3e0SJohn Polstra     if (phdyn == NULL) {
175bfb1ef60SJohn Polstra 	_rtld_error("%s: object is not dynamically-linked", path);
1763124c3e0SJohn Polstra 	return NULL;
1773124c3e0SJohn Polstra     }
1783124c3e0SJohn Polstra 
1798b7f25d4SAlexander Kabaev     if (nsegs < 0) {
180bfb1ef60SJohn Polstra 	_rtld_error("%s: too few PT_LOAD segments", path);
181bfb1ef60SJohn Polstra 	return NULL;
182bfb1ef60SJohn Polstra     }
1833124c3e0SJohn Polstra 
1843124c3e0SJohn Polstra     /*
1853124c3e0SJohn Polstra      * Map the entire address space of the object, to stake out our
1863124c3e0SJohn Polstra      * contiguous region, and to establish the base address for relocation.
1873124c3e0SJohn Polstra      */
1883124c3e0SJohn Polstra     base_offset = trunc_page(segs[0]->p_offset);
1893124c3e0SJohn Polstra     base_vaddr = trunc_page(segs[0]->p_vaddr);
1908b7f25d4SAlexander Kabaev     base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
1913124c3e0SJohn Polstra     mapsize = base_vlimit - base_vaddr;
1923124c3e0SJohn Polstra     base_addr = u.hdr.e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
1933124c3e0SJohn Polstra 
194fa7dd9c5SMatthew Dillon     mapbase = mmap(base_addr, mapsize, convert_prot(segs[0]->p_flags),
195fa7dd9c5SMatthew Dillon       convert_flags(segs[0]->p_flags), fd, base_offset);
1963124c3e0SJohn Polstra     if (mapbase == (caddr_t) -1) {
197bfb1ef60SJohn Polstra 	_rtld_error("%s: mmap of entire address space failed: %s",
198bfb1ef60SJohn Polstra 	  path, strerror(errno));
1993124c3e0SJohn Polstra 	return NULL;
2003124c3e0SJohn Polstra     }
2013124c3e0SJohn Polstra     if (base_addr != NULL && mapbase != base_addr) {
202bfb1ef60SJohn Polstra 	_rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
203bfb1ef60SJohn Polstra 	  path, base_addr, mapbase);
2043124c3e0SJohn Polstra 	munmap(mapbase, mapsize);
2053124c3e0SJohn Polstra 	return NULL;
2063124c3e0SJohn Polstra     }
2073124c3e0SJohn Polstra 
2088b7f25d4SAlexander Kabaev     for (i = 0; i <=  nsegs; i++) {
2098b7f25d4SAlexander Kabaev 	/* Overlay the segment onto the proper region. */
2108b7f25d4SAlexander Kabaev 	data_offset = trunc_page(segs[i]->p_offset);
2118b7f25d4SAlexander Kabaev 	data_vaddr = trunc_page(segs[i]->p_vaddr);
2128b7f25d4SAlexander Kabaev 	data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
2133124c3e0SJohn Polstra 	data_addr = mapbase + (data_vaddr - base_vaddr);
214fa7dd9c5SMatthew Dillon 	data_prot = convert_prot(segs[i]->p_flags);
215fa7dd9c5SMatthew Dillon 	data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
2168b7f25d4SAlexander Kabaev 	/* Do not call mmap on the first segment - this is redundant */
2178b7f25d4SAlexander Kabaev 	if (i && mmap(data_addr, data_vlimit - data_vaddr, data_prot,
218fa7dd9c5SMatthew Dillon 	  data_flags, fd, data_offset) == (caddr_t) -1) {
219bfb1ef60SJohn Polstra 	    _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
2203124c3e0SJohn Polstra 	    return NULL;
2213124c3e0SJohn Polstra 	}
2223124c3e0SJohn Polstra 
2238b7f25d4SAlexander Kabaev 	/* Clear any BSS in the last page of the segment. */
2248b7f25d4SAlexander Kabaev 	clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
2253124c3e0SJohn Polstra 	clear_addr = mapbase + (clear_vaddr - base_vaddr);
2268b7f25d4SAlexander Kabaev 	clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
2278b7f25d4SAlexander Kabaev 	if ((nclear = data_vlimit - clear_vaddr) > 0) {
2288b7f25d4SAlexander Kabaev 	    /* Make sure the end of the segment is writable */
2298b7f25d4SAlexander Kabaev 	    if ((data_prot & PROT_WRITE) == 0 &&
2308b7f25d4SAlexander Kabaev 		-1 ==  mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
2318b7f25d4SAlexander Kabaev 			_rtld_error("%s: mprotect failed: %s", path,
2328b7f25d4SAlexander Kabaev 			    strerror(errno));
2338b7f25d4SAlexander Kabaev 			return NULL;
2348b7f25d4SAlexander Kabaev 	    }
2358b7f25d4SAlexander Kabaev 
2363124c3e0SJohn Polstra 	    memset(clear_addr, 0, nclear);
2373124c3e0SJohn Polstra 
2388b7f25d4SAlexander Kabaev 	    /* Reset the data protection back */
2398b7f25d4SAlexander Kabaev 	    if ((data_prot & PROT_WRITE) == 0)
2408b7f25d4SAlexander Kabaev 		 mprotect(clear_page, PAGE_SIZE, data_prot);
2418b7f25d4SAlexander Kabaev 	}
2428b7f25d4SAlexander Kabaev 
2433124c3e0SJohn Polstra 	/* Overlay the BSS segment onto the proper region. */
2443124c3e0SJohn Polstra 	bss_vaddr = data_vlimit;
2458b7f25d4SAlexander Kabaev 	bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
2463124c3e0SJohn Polstra 	bss_addr = mapbase +  (bss_vaddr - base_vaddr);
2473124c3e0SJohn Polstra 	if (bss_vlimit > bss_vaddr) {	/* There is something to do */
2488b7f25d4SAlexander Kabaev 	    if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
2493124c3e0SJohn Polstra 		MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
2508b7f25d4SAlexander Kabaev 		    _rtld_error("%s: mmap of bss failed: %s", path,
2518b7f25d4SAlexander Kabaev 			strerror(errno));
2523124c3e0SJohn Polstra 		return NULL;
2533124c3e0SJohn Polstra 	    }
2543124c3e0SJohn Polstra 	}
2558b7f25d4SAlexander Kabaev     }
2563124c3e0SJohn Polstra 
257926ea445SJohn Polstra     obj = obj_new();
2587360ae0fSJohn Polstra     if (sb != NULL) {
2597360ae0fSJohn Polstra 	obj->dev = sb->st_dev;
2607360ae0fSJohn Polstra 	obj->ino = sb->st_ino;
2617360ae0fSJohn Polstra     }
2623124c3e0SJohn Polstra     obj->mapbase = mapbase;
2633124c3e0SJohn Polstra     obj->mapsize = mapsize;
2643124c3e0SJohn Polstra     obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
2653124c3e0SJohn Polstra       base_vaddr;
2663124c3e0SJohn Polstra     obj->vaddrbase = base_vaddr;
2673124c3e0SJohn Polstra     obj->relocbase = mapbase - base_vaddr;
268a607e5d7SJohn Polstra     obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
2693124c3e0SJohn Polstra     if (u.hdr.e_entry != 0)
270a607e5d7SJohn Polstra 	obj->entry = (caddr_t) (obj->relocbase + u.hdr.e_entry);
2713124c3e0SJohn Polstra     if (phphdr != NULL) {
272a607e5d7SJohn Polstra 	obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
2733124c3e0SJohn Polstra 	obj->phsize = phphdr->p_memsz;
2743124c3e0SJohn Polstra     }
275a607e5d7SJohn Polstra     if (phinterp != NULL)
276a607e5d7SJohn Polstra 	obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
2773124c3e0SJohn Polstra 
2783124c3e0SJohn Polstra     return obj;
2793124c3e0SJohn Polstra }
2803124c3e0SJohn Polstra 
281926ea445SJohn Polstra void
282926ea445SJohn Polstra obj_free(Obj_Entry *obj)
283926ea445SJohn Polstra {
284926ea445SJohn Polstra     Objlist_Entry *elm;
285926ea445SJohn Polstra 
286926ea445SJohn Polstra     free(obj->path);
287926ea445SJohn Polstra     while (obj->needed != NULL) {
288926ea445SJohn Polstra 	Needed_Entry *needed = obj->needed;
289926ea445SJohn Polstra 	obj->needed = needed->next;
290926ea445SJohn Polstra 	free(needed);
291926ea445SJohn Polstra     }
292926ea445SJohn Polstra     while (!STAILQ_EMPTY(&obj->dldags)) {
293926ea445SJohn Polstra 	elm = STAILQ_FIRST(&obj->dldags);
294926ea445SJohn Polstra 	STAILQ_REMOVE_HEAD(&obj->dldags, link);
295926ea445SJohn Polstra 	free(elm);
296926ea445SJohn Polstra     }
297926ea445SJohn Polstra     while (!STAILQ_EMPTY(&obj->dagmembers)) {
298926ea445SJohn Polstra 	elm = STAILQ_FIRST(&obj->dagmembers);
299926ea445SJohn Polstra 	STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
300926ea445SJohn Polstra 	free(elm);
301926ea445SJohn Polstra     }
302926ea445SJohn Polstra     free(obj);
303926ea445SJohn Polstra }
304926ea445SJohn Polstra 
305926ea445SJohn Polstra Obj_Entry *
306926ea445SJohn Polstra obj_new(void)
307926ea445SJohn Polstra {
308926ea445SJohn Polstra     Obj_Entry *obj;
309926ea445SJohn Polstra 
310926ea445SJohn Polstra     obj = CNEW(Obj_Entry);
311926ea445SJohn Polstra     STAILQ_INIT(&obj->dldags);
312926ea445SJohn Polstra     STAILQ_INIT(&obj->dagmembers);
313926ea445SJohn Polstra     return obj;
314926ea445SJohn Polstra }
315926ea445SJohn Polstra 
3163124c3e0SJohn Polstra /*
3173124c3e0SJohn Polstra  * Given a set of ELF protection flags, return the corresponding protection
3183124c3e0SJohn Polstra  * flags for MMAP.
3193124c3e0SJohn Polstra  */
3203124c3e0SJohn Polstra static int
321fa7dd9c5SMatthew Dillon convert_prot(int elfflags)
3223124c3e0SJohn Polstra {
3233124c3e0SJohn Polstra     int prot = 0;
3243124c3e0SJohn Polstra     if (elfflags & PF_R)
3253124c3e0SJohn Polstra 	prot |= PROT_READ;
3263124c3e0SJohn Polstra     if (elfflags & PF_W)
3273124c3e0SJohn Polstra 	prot |= PROT_WRITE;
3283124c3e0SJohn Polstra     if (elfflags & PF_X)
3293124c3e0SJohn Polstra 	prot |= PROT_EXEC;
3303124c3e0SJohn Polstra     return prot;
3313124c3e0SJohn Polstra }
332fa7dd9c5SMatthew Dillon 
333fa7dd9c5SMatthew Dillon static int
334fa7dd9c5SMatthew Dillon convert_flags(int elfflags)
335fa7dd9c5SMatthew Dillon {
336fa7dd9c5SMatthew Dillon     int flags = MAP_PRIVATE; /* All mappings are private */
337fa7dd9c5SMatthew Dillon 
338fa7dd9c5SMatthew Dillon     /*
339fa7dd9c5SMatthew Dillon      * Readonly mappings are marked "MAP_NOCORE", because they can be
340fa7dd9c5SMatthew Dillon      * reconstructed by a debugger.
341fa7dd9c5SMatthew Dillon      */
342fa7dd9c5SMatthew Dillon     if (!(elfflags & PF_W))
343fa7dd9c5SMatthew Dillon 	flags |= MAP_NOCORE;
344fa7dd9c5SMatthew Dillon     return flags;
345fa7dd9c5SMatthew Dillon }
346