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;
94 hdr = get_elf_header(fd, path);
99 * Scan the program header entries, and save key information.
101 * We expect that the loadable segments are ordered by load address.
103 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
104 phsize = hdr->e_phnum * sizeof (phdr[0]);
105 phlimit = phdr + hdr->e_phnum;
107 phdyn = phinterp = phtls = NULL;
114 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
115 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
116 while (phdr < phlimit) {
117 switch (phdr->p_type) {
124 segs[++nsegs] = phdr;
125 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
126 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
133 phdr_vaddr = phdr->p_vaddr;
134 phsize = phdr->p_memsz;
146 stack_flags = phdr->p_flags;
150 relro_page = phdr->p_vaddr;
151 relro_size = phdr->p_memsz;
155 if (phdr->p_offset > PAGE_SIZE ||
156 phdr->p_offset + phdr->p_filesz > PAGE_SIZE) {
157 note_map_len = round_page(phdr->p_offset +
158 phdr->p_filesz) - trunc_page(phdr->p_offset);
159 note_map = mmap(NULL, note_map_len, PROT_READ,
160 MAP_PRIVATE, fd, trunc_page(phdr->p_offset));
161 if (note_map == MAP_FAILED) {
162 _rtld_error("%s: error mapping PT_NOTE (%d)", path, errno);
165 note_start = (Elf_Addr)(note_map + phdr->p_offset -
166 trunc_page(phdr->p_offset));
168 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
170 note_end = note_start + phdr->p_filesz;
177 _rtld_error("%s: object is not dynamically-linked", path);
182 _rtld_error("%s: too few PT_LOAD segments", path);
187 * Map the entire address space of the object, to stake out our
188 * contiguous region, and to establish the base address for relocation.
190 base_vaddr = trunc_page(segs[0]->p_vaddr);
191 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
192 mapsize = base_vlimit - base_vaddr;
193 base_addr = (caddr_t) base_vaddr;
194 base_flags = MAP_PRIVATE | MAP_ANON | MAP_NOCORE;
195 if (npagesizes > 1 && round_page(segs[0]->p_filesz) >= pagesizes[1])
196 base_flags |= MAP_ALIGNED_SUPER;
198 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
199 if (mapbase == (caddr_t) -1) {
200 _rtld_error("%s: mmap of entire address space failed: %s",
201 path, rtld_strerror(errno));
204 if (base_addr != NULL && mapbase != base_addr) {
205 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
206 path, base_addr, mapbase);
210 for (i = 0; i <= nsegs; i++) {
211 /* Overlay the segment onto the proper region. */
212 data_offset = trunc_page(segs[i]->p_offset);
213 data_vaddr = trunc_page(segs[i]->p_vaddr);
214 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
215 data_addr = mapbase + (data_vaddr - base_vaddr);
216 data_prot = convert_prot(segs[i]->p_flags);
217 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
218 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
219 data_flags | MAP_PREFAULT_READ, fd, data_offset) == (caddr_t) -1) {
220 _rtld_error("%s: mmap of data failed: %s", path,
221 rtld_strerror(errno));
226 if (segs[i]->p_filesz != segs[i]->p_memsz) {
228 /* Clear any BSS in the last page of the segment. */
229 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
230 clear_addr = mapbase + (clear_vaddr - base_vaddr);
231 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
233 if ((nclear = data_vlimit - clear_vaddr) > 0) {
234 /* Make sure the end of the segment is writable */
235 if ((data_prot & PROT_WRITE) == 0 && -1 ==
236 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
237 _rtld_error("%s: mprotect failed: %s", path,
238 rtld_strerror(errno));
242 memset(clear_addr, 0, nclear);
244 /* Reset the data protection back */
245 if ((data_prot & PROT_WRITE) == 0)
246 mprotect(clear_page, PAGE_SIZE, data_prot);
249 /* Overlay the BSS segment onto the proper region. */
250 bss_vaddr = data_vlimit;
251 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
252 bss_addr = mapbase + (bss_vaddr - base_vaddr);
253 if (bss_vlimit > bss_vaddr) { /* There is something to do */
254 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
255 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
256 _rtld_error("%s: mmap of bss failed: %s", path,
257 rtld_strerror(errno));
263 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
264 (data_vlimit - data_vaddr + data_offset) >=
265 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
266 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
272 obj->dev = sb->st_dev;
273 obj->ino = sb->st_ino;
275 obj->mapbase = mapbase;
276 obj->mapsize = mapsize;
277 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
279 obj->vaddrbase = base_vaddr;
280 obj->relocbase = mapbase - base_vaddr;
281 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
282 if (hdr->e_entry != 0)
283 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
284 if (phdr_vaddr != 0) {
285 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr);
287 obj->phdr = malloc(phsize);
288 if (obj->phdr == NULL) {
290 _rtld_error("%s: cannot allocate program header", path);
293 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize);
294 obj->phdr_alloc = true;
296 obj->phsize = phsize;
297 if (phinterp != NULL)
298 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
300 tls_dtv_generation++;
301 obj->tlsindex = ++tls_max_index;
302 obj->tlssize = phtls->p_memsz;
303 obj->tlsalign = phtls->p_align;
304 obj->tlsinitsize = phtls->p_filesz;
305 obj->tlsinit = mapbase + phtls->p_vaddr;
307 obj->stack_flags = stack_flags;
308 obj->relro_page = obj->relocbase + trunc_page(relro_page);
309 obj->relro_size = round_page(relro_size);
310 if (note_start < note_end)
311 digest_notes(obj, note_start, note_end);
312 if (note_map != NULL)
313 munmap(note_map, note_map_len);
314 munmap(hdr, PAGE_SIZE);
318 munmap(mapbase, mapsize);
320 if (note_map != NULL && note_map != MAP_FAILED)
321 munmap(note_map, note_map_len);
322 munmap(hdr, PAGE_SIZE);
327 get_elf_header(int fd, const char *path)
331 hdr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
333 if (hdr == (Elf_Ehdr *)MAP_FAILED) {
334 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
338 /* Make sure the file is valid */
340 _rtld_error("%s: invalid file format", path);
343 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
344 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
345 _rtld_error("%s: unsupported file layout", path);
348 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
349 hdr->e_version != EV_CURRENT) {
350 _rtld_error("%s: unsupported file version", path);
353 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
354 _rtld_error("%s: unsupported file type", path);
357 if (hdr->e_machine != ELF_TARG_MACH) {
358 _rtld_error("%s: unsupported machine", path);
363 * We rely on the program header being in the first page. This is
364 * not strictly required by the ABI specification, but it seems to
365 * always true in practice. And, it simplifies things considerably.
367 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
369 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
372 if (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) >
374 _rtld_error("%s: program header too large", path);
380 munmap(hdr, PAGE_SIZE);
385 obj_free(Obj_Entry *obj)
390 free_tls_offset(obj);
391 while (obj->needed != NULL) {
392 Needed_Entry *needed = obj->needed;
393 obj->needed = needed->next;
396 while (!STAILQ_EMPTY(&obj->names)) {
397 Name_Entry *entry = STAILQ_FIRST(&obj->names);
398 STAILQ_REMOVE_HEAD(&obj->names, link);
401 while (!STAILQ_EMPTY(&obj->dldags)) {
402 elm = STAILQ_FIRST(&obj->dldags);
403 STAILQ_REMOVE_HEAD(&obj->dldags, link);
406 while (!STAILQ_EMPTY(&obj->dagmembers)) {
407 elm = STAILQ_FIRST(&obj->dagmembers);
408 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
413 if (obj->origin_path)
414 free(obj->origin_path);
422 free((void *)obj->phdr);
431 obj = CNEW(Obj_Entry);
432 STAILQ_INIT(&obj->dldags);
433 STAILQ_INIT(&obj->dagmembers);
434 STAILQ_INIT(&obj->names);
439 * Given a set of ELF protection flags, return the corresponding protection
443 convert_prot(int elfflags)
456 convert_flags(int elfflags)
458 int flags = MAP_PRIVATE; /* All mappings are private */
461 * Readonly mappings are marked "MAP_NOCORE", because they can be
462 * reconstructed by a debugger.
464 if (!(elfflags & PF_W))