2 * Copyright 1996-1998 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
25 * $FreeBSD: src/libexec/rtld-elf/map_object.c,v 1.7.2.2 2002/12/28 19:49:41 dillon Exp $
26 * $DragonFly: src/libexec/rtld-elf/map_object.c,v 1.2 2003/06/17 04:27:08 dillon Exp $
29 #include <sys/param.h>
41 static int convert_prot(int); /* Elf flags -> mmap protection */
42 static int convert_flags(int); /* Elf flags -> mmap flags */
45 * Map a shared object into memory. The "fd" argument is a file descriptor,
46 * which must be open on the object and positioned at its beginning.
47 * The "path" argument is a pathname that is used only for error messages.
49 * The return value is a pointer to a newly-allocated Obj_Entry structure
50 * for the shared object. Returns NULL on failure.
53 map_object(int fd, const char *path, const struct stat *sb)
88 if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
89 _rtld_error("%s: read error: %s", path, strerror(errno));
93 /* Make sure the file is valid */
94 if (nbytes < sizeof(Elf_Ehdr)
95 || u.hdr.e_ident[EI_MAG0] != ELFMAG0
96 || u.hdr.e_ident[EI_MAG1] != ELFMAG1
97 || u.hdr.e_ident[EI_MAG2] != ELFMAG2
98 || u.hdr.e_ident[EI_MAG3] != ELFMAG3) {
99 _rtld_error("%s: invalid file format", path);
102 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
103 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
104 _rtld_error("%s: unsupported file layout", path);
107 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
108 || u.hdr.e_version != EV_CURRENT) {
109 _rtld_error("%s: unsupported file version", path);
112 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
113 _rtld_error("%s: unsupported file type", path);
116 if (u.hdr.e_machine != ELF_TARG_MACH) {
117 _rtld_error("%s: unsupported machine", path);
122 * We rely on the program header being in the first page. This is
123 * not strictly required by the ABI specification, but it seems to
124 * always true in practice. And, it simplifies things considerably.
126 if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
128 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
131 if (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) > nbytes) {
132 _rtld_error("%s: program header too large", path);
137 * Scan the program header entries, and save key information.
139 * We rely on there being exactly two load segments, text and data,
142 phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff);
143 phlimit = phdr + u.hdr.e_phnum;
145 phdyn = phphdr = phinterp = NULL;
146 segs = alloca(sizeof(segs[0]) * u.hdr.e_phnum);
147 while (phdr < phlimit) {
148 switch (phdr->p_type) {
155 segs[++nsegs] = phdr;
156 if (segs[nsegs]->p_align < PAGE_SIZE) {
157 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
175 _rtld_error("%s: object is not dynamically-linked", path);
180 _rtld_error("%s: too few PT_LOAD segments", path);
185 * Map the entire address space of the object, to stake out our
186 * contiguous region, and to establish the base address for relocation.
188 base_offset = trunc_page(segs[0]->p_offset);
189 base_vaddr = trunc_page(segs[0]->p_vaddr);
190 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
191 mapsize = base_vlimit - base_vaddr;
192 base_addr = u.hdr.e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
194 mapbase = mmap(base_addr, mapsize, convert_prot(segs[0]->p_flags),
195 convert_flags(segs[0]->p_flags), fd, base_offset);
196 if (mapbase == (caddr_t) -1) {
197 _rtld_error("%s: mmap of entire address space failed: %s",
198 path, strerror(errno));
201 if (base_addr != NULL && mapbase != base_addr) {
202 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
203 path, base_addr, mapbase);
204 munmap(mapbase, mapsize);
208 for (i = 0; i <= nsegs; i++) {
209 /* Overlay the segment onto the proper region. */
210 data_offset = trunc_page(segs[i]->p_offset);
211 data_vaddr = trunc_page(segs[i]->p_vaddr);
212 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
213 data_addr = mapbase + (data_vaddr - base_vaddr);
214 data_prot = convert_prot(segs[i]->p_flags);
215 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
216 /* Do not call mmap on the first segment - this is redundant */
217 if (i && mmap(data_addr, data_vlimit - data_vaddr, data_prot,
218 data_flags, fd, data_offset) == (caddr_t) -1) {
219 _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
223 /* Clear any BSS in the last page of the segment. */
224 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
225 clear_addr = mapbase + (clear_vaddr - base_vaddr);
226 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
227 if ((nclear = data_vlimit - clear_vaddr) > 0) {
228 /* Make sure the end of the segment is writable */
229 if ((data_prot & PROT_WRITE) == 0 &&
230 -1 == mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
231 _rtld_error("%s: mprotect failed: %s", path,
236 memset(clear_addr, 0, nclear);
238 /* Reset the data protection back */
239 if ((data_prot & PROT_WRITE) == 0)
240 mprotect(clear_page, PAGE_SIZE, data_prot);
243 /* Overlay the BSS segment onto the proper region. */
244 bss_vaddr = data_vlimit;
245 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
246 bss_addr = mapbase + (bss_vaddr - base_vaddr);
247 if (bss_vlimit > bss_vaddr) { /* There is something to do */
248 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
249 MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
250 _rtld_error("%s: mmap of bss failed: %s", path,
259 obj->dev = sb->st_dev;
260 obj->ino = sb->st_ino;
262 obj->mapbase = mapbase;
263 obj->mapsize = mapsize;
264 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
266 obj->vaddrbase = base_vaddr;
267 obj->relocbase = mapbase - base_vaddr;
268 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
269 if (u.hdr.e_entry != 0)
270 obj->entry = (caddr_t) (obj->relocbase + u.hdr.e_entry);
271 if (phphdr != NULL) {
272 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
273 obj->phsize = phphdr->p_memsz;
275 if (phinterp != NULL)
276 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
282 obj_free(Obj_Entry *obj)
287 while (obj->needed != NULL) {
288 Needed_Entry *needed = obj->needed;
289 obj->needed = needed->next;
292 while (!STAILQ_EMPTY(&obj->dldags)) {
293 elm = STAILQ_FIRST(&obj->dldags);
294 STAILQ_REMOVE_HEAD(&obj->dldags, link);
297 while (!STAILQ_EMPTY(&obj->dagmembers)) {
298 elm = STAILQ_FIRST(&obj->dagmembers);
299 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
310 obj = CNEW(Obj_Entry);
311 STAILQ_INIT(&obj->dldags);
312 STAILQ_INIT(&obj->dagmembers);
317 * Given a set of ELF protection flags, return the corresponding protection
321 convert_prot(int elfflags)
334 convert_flags(int elfflags)
336 int flags = MAP_PRIVATE; /* All mappings are private */
339 * Readonly mappings are marked "MAP_NOCORE", because they can be
340 * reconstructed by a debugger.
342 if (!(elfflags & PF_W))