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 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)
82 size_t nclear, phsize;
89 hdr = get_elf_header(fd, path);
94 * Scan the program header entries, and save key information.
96 * We expect that the loadable segments are ordered by load address.
98 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
99 phsize = hdr->e_phnum * sizeof (phdr[0]);
100 phlimit = phdr + hdr->e_phnum;
102 phdyn = phinterp = phtls = NULL;
106 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
107 while (phdr < phlimit) {
108 switch (phdr->p_type) {
115 segs[++nsegs] = phdr;
116 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
117 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
124 phdr_vaddr = phdr->p_vaddr;
125 phsize = phdr->p_memsz;
137 relro_page = phdr->p_vaddr;
138 relro_size = phdr->p_memsz;
145 _rtld_error("%s: object is not dynamically-linked", path);
150 _rtld_error("%s: too few PT_LOAD segments", path);
155 * Map the entire address space of the object, to stake out our
156 * contiguous region, and to establish the base address for relocation.
158 base_offset = trunc_page(segs[0]->p_offset);
159 base_vaddr = trunc_page(segs[0]->p_vaddr);
160 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
161 mapsize = base_vlimit - base_vaddr;
162 base_addr = hdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
164 mapbase = mmap(base_addr, mapsize, PROT_NONE, MAP_ANON | MAP_PRIVATE |
166 if (mapbase == (caddr_t) -1) {
167 _rtld_error("%s: mmap of entire address space failed: %s",
168 path, strerror(errno));
171 if (base_addr != NULL && mapbase != base_addr) {
172 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
173 path, base_addr, mapbase);
174 munmap(mapbase, mapsize);
178 for (i = 0; i <= nsegs; i++) {
179 /* Overlay the segment onto the proper region. */
180 data_offset = trunc_page(segs[i]->p_offset);
181 data_vaddr = trunc_page(segs[i]->p_vaddr);
182 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
183 data_addr = mapbase + (data_vaddr - base_vaddr);
184 data_prot = convert_prot(segs[i]->p_flags);
185 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
186 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
187 data_flags, fd, data_offset) == (caddr_t) -1) {
188 _rtld_error("%s: mmap of data failed: %s", path, strerror(errno));
193 if (segs[i]->p_filesz != segs[i]->p_memsz) {
195 /* Clear any BSS in the last page of the segment. */
196 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
197 clear_addr = mapbase + (clear_vaddr - base_vaddr);
198 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
200 if ((nclear = data_vlimit - clear_vaddr) > 0) {
201 /* Make sure the end of the segment is writable */
202 if ((data_prot & PROT_WRITE) == 0 && -1 ==
203 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
204 _rtld_error("%s: mprotect failed: %s", path,
209 memset(clear_addr, 0, nclear);
212 * reset the data protection back, enable the segment to be
213 * coredumped since we modified it.
215 if ((data_prot & PROT_WRITE) == 0) {
216 madvise(clear_page, PAGE_SIZE, MADV_CORE);
217 mprotect(clear_page, PAGE_SIZE, data_prot);
221 /* Overlay the BSS segment onto the proper region. */
222 bss_vaddr = data_vlimit;
223 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
224 bss_addr = mapbase + (bss_vaddr - base_vaddr);
225 if (bss_vlimit > bss_vaddr) { /* There is something to do */
226 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
227 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
228 _rtld_error("%s: mmap of bss failed: %s", path,
235 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
236 (data_vlimit - data_vaddr + data_offset) >=
237 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
238 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
244 obj->dev = sb->st_dev;
245 obj->ino = sb->st_ino;
247 obj->mapbase = mapbase;
248 obj->mapsize = mapsize;
249 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
251 obj->vaddrbase = base_vaddr;
252 obj->relocbase = mapbase - base_vaddr;
253 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
254 if (hdr->e_entry != 0)
255 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
256 if (phdr_vaddr != 0) {
257 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr);
259 obj->phdr = malloc(phsize);
260 if (obj->phdr == NULL) {
262 _rtld_error("%s: cannot allocate program header", path);
265 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize);
266 obj->phdr_alloc = true;
268 obj->phsize = phsize;
269 if (phinterp != NULL)
270 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
272 tls_dtv_generation++;
273 obj->tlsindex = ++tls_max_index;
274 obj->tlssize = phtls->p_memsz;
275 obj->tlsalign = phtls->p_align;
276 obj->tlsinitsize = phtls->p_filesz;
277 obj->tlsinit = mapbase + phtls->p_vaddr;
281 obj->relro_page = obj->relocbase + trunc_page(relro_page);
282 obj->relro_size = round_page(relro_size);
288 get_elf_header (int fd, const char *path)
296 if ((nbytes = pread(fd, u.buf, PAGE_SIZE, 0)) == -1) {
297 _rtld_error("%s: read error: %s", path, strerror(errno));
301 /* Make sure the file is valid */
302 if (nbytes < (ssize_t)sizeof(Elf_Ehdr) || !IS_ELF(u.hdr)) {
303 _rtld_error("%s: invalid file format", path);
306 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
307 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
308 _rtld_error("%s: unsupported file layout", path);
311 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
312 || u.hdr.e_version != EV_CURRENT) {
313 _rtld_error("%s: unsupported file version", path);
316 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
317 _rtld_error("%s: unsupported file type", path);
320 if (u.hdr.e_machine != ELF_TARG_MACH) {
321 _rtld_error("%s: unsupported machine", path);
326 * We rely on the program header being in the first page. This is
327 * not strictly required by the ABI specification, but it seems to
328 * always true in practice. And, it simplifies things considerably.
330 if (u.hdr.e_phentsize != sizeof(Elf_Phdr)) {
332 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
335 if (u.hdr.e_phoff + u.hdr.e_phnum * sizeof(Elf_Phdr) > (size_t)nbytes) {
336 _rtld_error("%s: program header too large", path);
344 obj_free(Obj_Entry *obj)
349 free_tls_offset(obj);
350 while (obj->needed != NULL) {
351 Needed_Entry *needed = obj->needed;
352 obj->needed = needed->next;
355 while (!STAILQ_EMPTY(&obj->names)) {
356 Name_Entry *entry = STAILQ_FIRST(&obj->names);
357 STAILQ_REMOVE_HEAD(&obj->names, link);
360 while (!STAILQ_EMPTY(&obj->dldags)) {
361 elm = STAILQ_FIRST(&obj->dldags);
362 STAILQ_REMOVE_HEAD(&obj->dldags, link);
365 while (!STAILQ_EMPTY(&obj->dagmembers)) {
366 elm = STAILQ_FIRST(&obj->dagmembers);
367 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
372 if (obj->origin_path)
373 free(obj->origin_path);
381 free((void *)obj->phdr);
390 obj = CNEW(Obj_Entry);
391 STAILQ_INIT(&obj->dldags);
392 STAILQ_INIT(&obj->dagmembers);
393 STAILQ_INIT(&obj->names);
398 * Given a set of ELF protection flags, return the corresponding protection
402 convert_prot(int elfflags)
415 convert_flags(int elfflags)
417 int flags = MAP_PRIVATE; /* All mappings are private */
420 * Readonly mappings are marked "MAP_NOCORE", because they can be
421 * reconstructed by a debugger.
423 if (!(elfflags & PF_W))