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.4 2004/11/18 10:01:47 dillon Exp $
29 #include <sys/param.h>
41 static Elf_Ehdr *get_elf_header (int, const char *);
42 static int convert_prot(int); /* Elf flags -> mmap protection */
43 static int convert_flags(int); /* Elf flags -> mmap flags */
46 * Map a shared object into memory. The "fd" argument is a file descriptor,
47 * which must be open on the object and positioned at its beginning.
48 * The "path" argument is a pathname that is used only for error messages.
50 * The return value is a pointer to a newly-allocated Obj_Entry structure
51 * for the shared object. Returns NULL on failure.
54 map_object(int fd, const char *path, const struct stat *sb)
86 hdr = get_elf_header(fd, path);
91 * Scan the program header entries, and save key information.
93 * We rely on there being exactly two load segments, text and data,
96 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
97 phlimit = phdr + hdr->e_phnum;
99 phdyn = phphdr = phinterp = NULL;
100 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
101 while (phdr < phlimit) {
102 switch (phdr->p_type) {
109 segs[++nsegs] = phdr;
110 if (segs[nsegs]->p_align < PAGE_SIZE) {
111 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
129 _rtld_error("%s: object is not dynamically-linked", path);
134 _rtld_error("%s: too few PT_LOAD segments", path);
139 * Map the entire address space of the object, to stake out our
140 * contiguous region, and to establish the base address for relocation.
142 base_offset = trunc_page(segs[0]->p_offset);
143 base_vaddr = trunc_page(segs[0]->p_vaddr);
144 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
145 mapsize = base_vlimit - base_vaddr;
146 base_addr = hdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
148 mapbase = mmap(base_addr, mapsize, convert_prot(segs[0]->p_flags),
149 convert_flags(segs[0]->p_flags), fd, base_offset);
150 if (mapbase == (caddr_t) -1) {
151 _rtld_error("%s: mmap of entire address space failed: %s",
152 path, strerror(errno));
155 if (base_addr != NULL && mapbase != base_addr) {
156 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
157 path, base_addr, mapbase);
158 munmap(mapbase, mapsize);
162 for (i = 0; i <= nsegs; i++) {
163 /* Overlay the segment onto the proper region. */
164 data_offset = trunc_page(segs[i]->p_offset);
165 data_vaddr = trunc_page(segs[i]->p_vaddr);
166 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
167 data_addr = mapbase + (data_vaddr - base_vaddr);
168 data_prot = convert_prot(segs[i]->p_flags);
169 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
170 /* Do not call mmap on the first segment - this is redundant */
171 if (i && mmap(data_addr, data_vlimit - data_vaddr, data_prot,
172 data_flags, fd, data_offset) == (caddr_t) -1) {
173 _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
177 /* Clear any BSS in the last page of the segment. */
178 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
179 clear_addr = mapbase + (clear_vaddr - base_vaddr);
180 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
181 if ((nclear = data_vlimit - clear_vaddr) > 0) {
182 /* Make sure the end of the segment is writable */
183 if ((data_prot & PROT_WRITE) == 0) {
184 if (mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE) < 0) {
185 _rtld_error("%s: mprotect failed: %s", path,
191 memset(clear_addr, 0, nclear);
194 * reset the data protection back, enable the segment to be
195 * coredumped since we modified it.
197 if ((data_prot & PROT_WRITE) == 0) {
198 madvise(clear_page, PAGE_SIZE, MADV_CORE);
199 mprotect(clear_page, PAGE_SIZE, data_prot);
203 /* Overlay the BSS segment onto the proper region. */
204 bss_vaddr = data_vlimit;
205 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->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, data_prot,
209 MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
210 _rtld_error("%s: mmap of bss failed: %s", path,
219 obj->dev = sb->st_dev;
220 obj->ino = sb->st_ino;
222 obj->mapbase = mapbase;
223 obj->mapsize = mapsize;
224 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
226 obj->vaddrbase = base_vaddr;
227 obj->relocbase = mapbase - base_vaddr;
228 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
229 if (hdr->e_entry != 0)
230 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
231 if (phphdr != NULL) {
232 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
233 obj->phsize = phphdr->p_memsz;
235 if (phinterp != NULL)
236 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
242 get_elf_header (int fd, const char *path)
250 if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
251 _rtld_error("%s: read error: %s", path, strerror(errno));
255 /* Make sure the file is valid */
256 if (nbytes < (ssize_t)sizeof(Elf_Ehdr) || !IS_ELF(u.hdr)) {
257 _rtld_error("%s: invalid file format", path);
260 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
261 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
262 _rtld_error("%s: unsupported file layout", path);
265 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
266 || u.hdr.e_version != EV_CURRENT) {
267 _rtld_error("%s: unsupported file version", path);
270 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
271 _rtld_error("%s: unsupported file type", path);
274 if (u.hdr.e_machine != ELF_TARG_MACH) {
275 _rtld_error("%s: unsupported machine", path);
280 * We rely on the program header being in the first page. This is
281 * not strictly required by the ABI specification, but it seems to
282 * always true in practice. And, it simplifies things considerably.
284 if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
286 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
289 if (u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) > (size_t)nbytes) {
290 _rtld_error("%s: program header too large", path);
298 obj_free(Obj_Entry *obj)
303 while (obj->needed != NULL) {
304 Needed_Entry *needed = obj->needed;
305 obj->needed = needed->next;
308 while (!STAILQ_EMPTY(&obj->dldags)) {
309 elm = STAILQ_FIRST(&obj->dldags);
310 STAILQ_REMOVE_HEAD(&obj->dldags, link);
313 while (!STAILQ_EMPTY(&obj->dagmembers)) {
314 elm = STAILQ_FIRST(&obj->dagmembers);
315 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
326 obj = CNEW(Obj_Entry);
327 STAILQ_INIT(&obj->dldags);
328 STAILQ_INIT(&obj->dagmembers);
333 * Given a set of ELF protection flags, return the corresponding protection
337 convert_prot(int elfflags)
350 convert_flags(int elfflags)
352 int flags = MAP_PRIVATE; /* All mappings are private */
355 * Readonly mappings are marked "MAP_NOCORE", because they can be
356 * reconstructed by a debugger.
358 if (!(elfflags & PF_W))