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.
28 #include <sys/param.h>
41 static int protflags(int); /* Elf flags -> mmap protection */
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)
84 if ((nbytes = read(fd, u.buf, PAGE_SIZE)) == -1) {
85 _rtld_error("%s: read error: %s", path, strerror(errno));
89 /* Make sure the file is valid */
90 if (nbytes < sizeof(Elf_Ehdr)
91 || u.hdr.e_ident[EI_MAG0] != ELFMAG0
92 || u.hdr.e_ident[EI_MAG1] != ELFMAG1
93 || u.hdr.e_ident[EI_MAG2] != ELFMAG2
94 || u.hdr.e_ident[EI_MAG3] != ELFMAG3) {
95 _rtld_error("%s: invalid file format", path);
98 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
99 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
100 _rtld_error("%s: unsupported file layout", path);
103 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
104 || u.hdr.e_version != EV_CURRENT) {
105 _rtld_error("%s: unsupported file version", path);
108 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
109 _rtld_error("%s: unsupported file type", path);
112 if (u.hdr.e_machine != ELF_TARG_MACH) {
113 _rtld_error("%s: unsupported machine", path);
118 * We rely on the program header being in the first page. This is
119 * not strictly required by the ABI specification, but it seems to
120 * always true in practice. And, it simplifies things considerably.
122 if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
124 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
127 if (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) > nbytes) {
128 _rtld_error("%s: program header too large", path);
133 * Scan the program header entries, and save key information.
135 * We rely on there being exactly two load segments, text and data,
138 phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff);
139 phlimit = phdr + u.hdr.e_phnum;
141 phdyn = phphdr = phinterp = NULL;
142 while (phdr < phlimit) {
143 switch (phdr->p_type) {
151 _rtld_error("%s: too many PT_LOAD segments", path);
170 _rtld_error("%s: object is not dynamically-linked", path);
175 _rtld_error("%s: too few PT_LOAD segments", path);
178 if (segs[0]->p_align < PAGE_SIZE || segs[1]->p_align < PAGE_SIZE) {
179 _rtld_error("%s: PT_LOAD segments not page-aligned", 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[1]->p_vaddr + segs[1]->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, protflags(segs[0]->p_flags),
194 MAP_PRIVATE, 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 /* Overlay the data segment onto the proper region. */
208 data_offset = trunc_page(segs[1]->p_offset);
209 data_vaddr = trunc_page(segs[1]->p_vaddr);
210 data_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_filesz);
211 data_addr = mapbase + (data_vaddr - base_vaddr);
212 if (mmap(data_addr, data_vlimit - data_vaddr, protflags(segs[1]->p_flags),
213 MAP_PRIVATE|MAP_FIXED, fd, data_offset) == (caddr_t) -1) {
214 _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
218 /* Clear any BSS in the last page of the data segment. */
219 clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
220 clear_addr = mapbase + (clear_vaddr - base_vaddr);
221 if ((nclear = data_vlimit - clear_vaddr) > 0)
222 memset(clear_addr, 0, nclear);
224 /* Overlay the BSS segment onto the proper region. */
225 bss_vaddr = data_vlimit;
226 bss_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
227 bss_addr = mapbase + (bss_vaddr - base_vaddr);
228 if (bss_vlimit > bss_vaddr) { /* There is something to do */
229 if (mmap(bss_addr, bss_vlimit - bss_vaddr, protflags(segs[1]->p_flags),
230 MAP_PRIVATE|MAP_FIXED|MAP_ANON, -1, 0) == (caddr_t) -1) {
231 _rtld_error("%s: mmap of bss failed: %s", path, strerror(errno));
238 obj->dev = sb->st_dev;
239 obj->ino = sb->st_ino;
241 obj->mapbase = mapbase;
242 obj->mapsize = mapsize;
243 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
245 obj->vaddrbase = base_vaddr;
246 obj->relocbase = mapbase - base_vaddr;
247 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
248 if (u.hdr.e_entry != 0)
249 obj->entry = (caddr_t) (obj->relocbase + u.hdr.e_entry);
250 if (phphdr != NULL) {
251 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phphdr->p_vaddr);
252 obj->phsize = phphdr->p_memsz;
254 if (phinterp != NULL)
255 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
261 obj_free(Obj_Entry *obj)
266 while (obj->needed != NULL) {
267 Needed_Entry *needed = obj->needed;
268 obj->needed = needed->next;
271 while (!STAILQ_EMPTY(&obj->dldags)) {
272 elm = STAILQ_FIRST(&obj->dldags);
273 STAILQ_REMOVE_HEAD(&obj->dldags, link);
276 while (!STAILQ_EMPTY(&obj->dagmembers)) {
277 elm = STAILQ_FIRST(&obj->dagmembers);
278 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
289 obj = CNEW(Obj_Entry);
290 STAILQ_INIT(&obj->dldags);
291 STAILQ_INIT(&obj->dagmembers);
296 * Given a set of ELF protection flags, return the corresponding protection
300 protflags(int elfflags)