xref: /freebsd/libexec/rtld-elf/map_object.c (revision 11afcc8f9f96d657b8e6f7547c02c1957331fc96)
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  *      $Id: map_object.c,v 1.2 1998/03/06 22:14:53 jdp Exp $
26  */
27 
28 #include <sys/param.h>
29 #include <sys/mman.h>
30 
31 #include <assert.h>
32 #include <errno.h>
33 #include <stddef.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include "rtld.h"
38 
39 static int protflags(int);	/* Elf flags -> mmap protection */
40 
41 /*
42  * Map a shared object into memory.  The argument is a file descriptor,
43  * which must be open on the object and positioned at its beginning.
44  *
45  * The return value is a pointer to a newly-allocated Obj_Entry structure
46  * for the shared object.  Returns NULL on failure.
47  */
48 Obj_Entry *
49 map_object(int fd)
50 {
51     Obj_Entry *obj;
52     union {
53 	Elf32_Ehdr hdr;
54 	char buf[PAGE_SIZE];
55     } u;
56     int nbytes;
57     Elf32_Phdr *phdr;
58     Elf32_Phdr *phlimit;
59     Elf32_Phdr *segs[2];
60     int nsegs;
61     Elf32_Phdr *phdyn;
62     Elf32_Phdr *phphdr;
63     caddr_t mapbase;
64     size_t mapsize;
65     Elf32_Off base_offset;
66     Elf32_Addr base_vaddr;
67     Elf32_Addr base_vlimit;
68     caddr_t base_addr;
69     Elf32_Off data_offset;
70     Elf32_Addr data_vaddr;
71     Elf32_Addr data_vlimit;
72     caddr_t data_addr;
73     Elf32_Addr clear_vaddr;
74     caddr_t clear_addr;
75     size_t nclear;
76     Elf32_Addr bss_vaddr;
77     Elf32_Addr bss_vlimit;
78     caddr_t bss_addr;
79 
80     if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
81 	_rtld_error("Read error: %s", strerror(errno));
82 	return NULL;
83     }
84 
85     /* Make sure the file is valid */
86     if (nbytes < sizeof(Elf32_Ehdr)
87       || u.hdr.e_ident[EI_MAG0] != ELFMAG0
88       || u.hdr.e_ident[EI_MAG1] != ELFMAG1
89       || u.hdr.e_ident[EI_MAG2] != ELFMAG2
90       || u.hdr.e_ident[EI_MAG3] != ELFMAG3) {
91 	_rtld_error("Invalid file format");
92 	return NULL;
93     }
94     if (u.hdr.e_ident[EI_CLASS] != ELFCLASS32
95       || u.hdr.e_ident[EI_DATA] != ELFDATA2LSB) {
96 	_rtld_error("Unsupported file layout");
97 	return NULL;
98     }
99     if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
100       || u.hdr.e_version != EV_CURRENT) {
101 	_rtld_error("Unsupported file version");
102 	return NULL;
103     }
104     if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
105 	_rtld_error("Unsupported file type");
106 	return NULL;
107     }
108     if (u.hdr.e_machine != EM_386) {
109 	_rtld_error("Unsupported machine");
110 	return NULL;
111     }
112 
113     /*
114      * We rely on the program header being in the first page.  This is
115      * not strictly required by the ABI specification, but it seems to
116      * always true in practice.  And, it simplifies things considerably.
117      */
118     assert(u.hdr.e_phentsize == sizeof(Elf32_Phdr));
119     assert(u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf32_Phdr) <= PAGE_SIZE);
120     assert(u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf32_Phdr) <= nbytes);
121 
122     /*
123      * Scan the program header entries, and save key information.
124      *
125      * We rely on there being exactly two load segments, text and data,
126      * in that order.
127      */
128     phdr = (Elf32_Phdr *) (u.buf + u.hdr.e_phoff);
129     phlimit = phdr + u.hdr.e_phnum;
130     nsegs = 0;
131     phdyn = NULL;
132     phphdr = NULL;
133     while (phdr < phlimit) {
134 	switch (phdr->p_type) {
135 
136 	case PT_LOAD:
137 	    assert(nsegs < 2);
138 	    segs[nsegs] = phdr;
139 	    ++nsegs;
140 	    break;
141 
142 	case PT_PHDR:
143 	    phphdr = phdr;
144 	    break;
145 
146 	case PT_DYNAMIC:
147 	    phdyn = phdr;
148 	    break;
149 	}
150 
151 	++phdr;
152     }
153     if (phdyn == NULL) {
154 	_rtld_error("Object is not dynamically-linked");
155 	return NULL;
156     }
157 
158     assert(nsegs == 2);
159     assert(segs[0]->p_align <= PAGE_SIZE);
160     assert(segs[1]->p_align <= PAGE_SIZE);
161 
162     /*
163      * Map the entire address space of the object, to stake out our
164      * contiguous region, and to establish the base address for relocation.
165      */
166     base_offset = trunc_page(segs[0]->p_offset);
167     base_vaddr = trunc_page(segs[0]->p_vaddr);
168     base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
169     mapsize = base_vlimit - base_vaddr;
170     base_addr = u.hdr.e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
171 
172     mapbase = mmap(base_addr, mapsize, protflags(segs[0]->p_flags),
173       MAP_PRIVATE, fd, base_offset);
174     if (mapbase == (caddr_t) -1) {
175 	_rtld_error("mmap of entire address space failed: %s",
176 	  strerror(errno));
177 	return NULL;
178     }
179     if (base_addr != NULL && mapbase != base_addr) {
180 	_rtld_error("mmap returned wrong address: wanted %p, got %p",
181 	  base_addr, mapbase);
182 	munmap(mapbase, mapsize);
183 	return NULL;
184     }
185 
186     /* Overlay the data segment onto the proper region. */
187     data_offset = trunc_page(segs[1]->p_offset);
188     data_vaddr = trunc_page(segs[1]->p_vaddr);
189     data_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_filesz);
190     data_addr = mapbase + (data_vaddr - base_vaddr);
191     if (mmap(data_addr, data_vlimit - data_vaddr, protflags(segs[1]->p_flags),
192       MAP_PRIVATE|MAP_FIXED, fd, data_offset) == (caddr_t) -1) {
193 	_rtld_error("mmap of data failed: %s", strerror(errno));
194 	return NULL;
195     }
196 
197     /* Clear any BSS in the last page of the data segment. */
198     clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
199     clear_addr = mapbase + (clear_vaddr - base_vaddr);
200     if ((nclear = data_vlimit - clear_vaddr) > 0)
201 	memset(clear_addr, 0, nclear);
202 
203     /* Overlay the BSS segment onto the proper region. */
204     bss_vaddr = data_vlimit;
205     bss_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
206     bss_addr = mapbase +  (bss_vaddr - base_vaddr);
207     if (bss_vlimit > bss_vaddr) {	/* There is something to do */
208 	if (mmap(bss_addr, bss_vlimit - bss_vaddr, protflags(segs[1]->p_flags),
209 	  MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
210 	    _rtld_error("mmap of bss failed: %s", strerror(errno));
211 	    return NULL;
212 	}
213     }
214 
215     obj = CNEW(Obj_Entry);
216     obj->mapbase = mapbase;
217     obj->mapsize = mapsize;
218     obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
219       base_vaddr;
220     obj->vaddrbase = base_vaddr;
221     obj->relocbase = mapbase - base_vaddr;
222     obj->dynamic = (const Elf32_Dyn *)
223       (mapbase + (phdyn->p_vaddr - base_vaddr));
224     if (u.hdr.e_entry != 0)
225 	obj->entry = (caddr_t) (mapbase + (u.hdr.e_entry - base_vaddr));
226     if (phphdr != NULL) {
227 	obj->phdr = (const Elf32_Phdr *)
228 	  (mapbase + (phphdr->p_vaddr - base_vaddr));
229 	obj->phsize = phphdr->p_memsz;
230     }
231 
232     return obj;
233 }
234 
235 /*
236  * Given a set of ELF protection flags, return the corresponding protection
237  * flags for MMAP.
238  */
239 static int
240 protflags(int elfflags)
241 {
242     int prot = 0;
243     if (elfflags & PF_R)
244 	prot |= PROT_READ;
245     if (elfflags & PF_W)
246 	prot |= PROT_WRITE;
247     if (elfflags & PF_X)
248 	prot |= PROT_EXEC;
249     return prot;
250 }
251