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 $
28 #include <sys/param.h>
40 static int convert_prot(int); /* Elf flags -> mmap protection */
41 static int convert_flags(int); /* Elf flags -> mmap flags */
44 * Map a shared object into memory. The "fd" argument is a file descriptor,
45 * which must be open on the object and positioned at its beginning.
46 * The "path" argument is a pathname that is used only for error messages.
48 * The return value is a pointer to a newly-allocated Obj_Entry structure
49 * for the shared object. Returns NULL on failure.
52 map_object(int fd, const char *path, const struct stat *sb)
87 if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
88 _rtld_error("%s: read error: %s", path, strerror(errno));
92 /* Make sure the file is valid */
93 if (nbytes < sizeof(Elf_Ehdr)
94 || u.hdr.e_ident[EI_MAG0] != ELFMAG0
95 || u.hdr.e_ident[EI_MAG1] != ELFMAG1
96 || u.hdr.e_ident[EI_MAG2] != ELFMAG2
97 || u.hdr.e_ident[EI_MAG3] != ELFMAG3) {
98 _rtld_error("%s: invalid file format", path);
101 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
102 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
103 _rtld_error("%s: unsupported file layout", path);
106 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
107 || u.hdr.e_version != EV_CURRENT) {
108 _rtld_error("%s: unsupported file version", path);
111 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
112 _rtld_error("%s: unsupported file type", path);
115 if (u.hdr.e_machine != ELF_TARG_MACH) {
116 _rtld_error("%s: unsupported machine", path);
121 * We rely on the program header being in the first page. This is
122 * not strictly required by the ABI specification, but it seems to
123 * always true in practice. And, it simplifies things considerably.
125 if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
127 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
130 if (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) > nbytes) {
131 _rtld_error("%s: program header too large", path);
136 * Scan the program header entries, and save key information.
138 * We rely on there being exactly two load segments, text and data,
141 phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff);
142 phlimit = phdr + u.hdr.e_phnum;
144 phdyn = phphdr = phinterp = NULL;
145 segs = alloca(sizeof(segs[0]) * u.hdr.e_phnum);
146 while (phdr < phlimit) {
147 switch (phdr->p_type) {
154 segs[++nsegs] = phdr;
155 if (segs[nsegs]->p_align < PAGE_SIZE) {
156 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
174 _rtld_error("%s: object is not dynamically-linked", path);
179 _rtld_error("%s: too few PT_LOAD segments", path);
184 * Map the entire address space of the object, to stake out our
185 * contiguous region, and to establish the base address for relocation.
187 base_offset = trunc_page(segs[0]->p_offset);
188 base_vaddr = trunc_page(segs[0]->p_vaddr);
189 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
190 mapsize = base_vlimit - base_vaddr;
191 base_addr = u.hdr.e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
193 mapbase = mmap(base_addr, mapsize, convert_prot(segs[0]->p_flags),
194 convert_flags(segs[0]->p_flags), fd, base_offset);
195 if (mapbase == (caddr_t) -1) {
196 _rtld_error("%s: mmap of entire address space failed: %s",
197 path, strerror(errno));
200 if (base_addr != NULL && mapbase != base_addr) {
201 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
202 path, base_addr, mapbase);
203 munmap(mapbase, mapsize);
207 for (i = 0; i <= nsegs; i++) {
208 /* Overlay the segment onto the proper region. */
209 data_offset = trunc_page(segs[i]->p_offset);
210 data_vaddr = trunc_page(segs[i]->p_vaddr);
211 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
212 data_addr = mapbase + (data_vaddr - base_vaddr);
213 data_prot = convert_prot(segs[i]->p_flags);
214 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
215 /* Do not call mmap on the first segment - this is redundant */
216 if (i && mmap(data_addr, data_vlimit - data_vaddr, data_prot,
217 data_flags, fd, data_offset) == (caddr_t) -1) {
218 _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
222 /* Clear any BSS in the last page of the segment. */
223 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
224 clear_addr = mapbase + (clear_vaddr - base_vaddr);
225 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
226 if ((nclear = data_vlimit - clear_vaddr) > 0) {
227 /* Make sure the end of the segment is writable */
228 if ((data_prot & PROT_WRITE) == 0 &&
229 -1 == mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
230 _rtld_error("%s: mprotect failed: %s", path,
235 memset(clear_addr, 0, nclear);
237 /* Reset the data protection back */
238 if ((data_prot & PROT_WRITE) == 0)
239 mprotect(clear_page, PAGE_SIZE, data_prot);
242 /* Overlay the BSS segment onto the proper region. */
243 bss_vaddr = data_vlimit;
244 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
245 bss_addr = mapbase + (bss_vaddr - base_vaddr);
246 if (bss_vlimit > bss_vaddr) { /* There is something to do */
247 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
248 MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
249 _rtld_error("%s: mmap of bss failed: %s", path,
258 obj->dev = sb->st_dev;
259 obj->ino = sb->st_ino;
261 obj->mapbase = mapbase;
262 obj->mapsize = mapsize;
263 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
265 obj->vaddrbase = base_vaddr;
266 obj->relocbase = mapbase - base_vaddr;
267 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
268 if (u.hdr.e_entry != 0)
269 obj->entry = (caddr_t) (obj->relocbase + u.hdr.e_entry);
270 if (phphdr != NULL) {
271 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
272 obj->phsize = phphdr->p_memsz;
274 if (phinterp != NULL)
275 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
281 obj_free(Obj_Entry *obj)
286 while (obj->needed != NULL) {
287 Needed_Entry *needed = obj->needed;
288 obj->needed = needed->next;
291 while (!STAILQ_EMPTY(&obj->dldags)) {
292 elm = STAILQ_FIRST(&obj->dldags);
293 STAILQ_REMOVE_HEAD(&obj->dldags, link);
296 while (!STAILQ_EMPTY(&obj->dagmembers)) {
297 elm = STAILQ_FIRST(&obj->dagmembers);
298 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
309 obj = CNEW(Obj_Entry);
310 STAILQ_INIT(&obj->dldags);
311 STAILQ_INIT(&obj->dagmembers);
316 * Given a set of ELF protection flags, return the corresponding protection
320 convert_prot(int elfflags)
333 convert_flags(int elfflags)
335 int flags = MAP_PRIVATE; /* All mappings are private */
338 * Readonly mappings are marked "MAP_NOCORE", because they can be
339 * reconstructed by a debugger.
341 if (!(elfflags & PF_W))