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_flags(int); /* Elf flags -> mmap flags */
45 * Map a shared object into memory. The "fd" argument is a file descriptor,
46 * which must be open on the object and positioned at its beginning.
47 * The "path" argument is a pathname that is used only for error messages.
49 * The return value is a pointer to a newly-allocated Obj_Entry structure
50 * for the shared object. Returns NULL on failure.
53 map_object(int fd, const char *path, const struct stat *sb)
81 size_t nclear, phsize;
93 hdr = get_elf_header(fd, path);
98 * Scan the program header entries, and save key information.
100 * We expect that the loadable segments are ordered by load address.
102 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
103 phsize = hdr->e_phnum * sizeof (phdr[0]);
104 phlimit = phdr + hdr->e_phnum;
106 phdyn = phinterp = phtls = NULL;
113 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
114 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
115 while (phdr < phlimit) {
116 switch (phdr->p_type) {
123 segs[++nsegs] = phdr;
124 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
125 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
132 phdr_vaddr = phdr->p_vaddr;
133 phsize = phdr->p_memsz;
145 stack_flags = phdr->p_flags;
149 relro_page = phdr->p_vaddr;
150 relro_size = phdr->p_memsz;
154 if (phdr->p_offset > PAGE_SIZE ||
155 phdr->p_offset + phdr->p_filesz > PAGE_SIZE) {
156 note_map_len = round_page(phdr->p_offset +
157 phdr->p_filesz) - trunc_page(phdr->p_offset);
158 note_map = mmap(NULL, note_map_len, PROT_READ,
159 MAP_PRIVATE, fd, trunc_page(phdr->p_offset));
160 if (note_map == MAP_FAILED) {
161 _rtld_error("%s: error mapping PT_NOTE (%d)", path, errno);
164 note_start = (Elf_Addr)(note_map + phdr->p_offset -
165 trunc_page(phdr->p_offset));
167 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
169 note_end = note_start + phdr->p_filesz;
176 _rtld_error("%s: object is not dynamically-linked", path);
181 _rtld_error("%s: too few PT_LOAD segments", path);
186 * Map the entire address space of the object, to stake out our
187 * contiguous region, and to establish the base address for relocation.
189 base_vaddr = trunc_page(segs[0]->p_vaddr);
190 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
191 mapsize = base_vlimit - base_vaddr;
192 base_addr = (caddr_t) base_vaddr;
193 base_flags = MAP_PRIVATE | MAP_ANON | MAP_NOCORE;
194 if (npagesizes > 1 && round_page(segs[0]->p_filesz) >= pagesizes[1])
195 base_flags |= MAP_ALIGNED_SUPER;
197 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
198 if (mapbase == (caddr_t) -1) {
199 _rtld_error("%s: mmap of entire address space failed: %s",
200 path, rtld_strerror(errno));
203 if (base_addr != NULL && mapbase != base_addr) {
204 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
205 path, base_addr, mapbase);
209 for (i = 0; i <= nsegs; i++) {
210 /* Overlay the segment onto the proper region. */
211 data_offset = trunc_page(segs[i]->p_offset);
212 data_vaddr = trunc_page(segs[i]->p_vaddr);
213 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
214 data_addr = mapbase + (data_vaddr - base_vaddr);
215 data_prot = convert_prot(segs[i]->p_flags);
216 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
217 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
218 data_flags | MAP_PREFAULT_READ, fd, data_offset) == (caddr_t) -1) {
219 _rtld_error("%s: mmap of data failed: %s", path,
220 rtld_strerror(errno));
225 if (segs[i]->p_filesz != segs[i]->p_memsz) {
227 /* Clear any BSS in the last page of the segment. */
228 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
229 clear_addr = mapbase + (clear_vaddr - base_vaddr);
230 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
232 if ((nclear = data_vlimit - clear_vaddr) > 0) {
233 /* Make sure the end of the segment is writable */
234 if ((data_prot & PROT_WRITE) == 0 && -1 ==
235 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
236 _rtld_error("%s: mprotect failed: %s", path,
237 rtld_strerror(errno));
241 memset(clear_addr, 0, nclear);
243 /* Reset the data protection back */
244 if ((data_prot & PROT_WRITE) == 0)
245 mprotect(clear_page, PAGE_SIZE, data_prot);
248 /* Overlay the BSS segment onto the proper region. */
249 bss_vaddr = data_vlimit;
250 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
251 bss_addr = mapbase + (bss_vaddr - base_vaddr);
252 if (bss_vlimit > bss_vaddr) { /* There is something to do */
253 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
254 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
255 _rtld_error("%s: mmap of bss failed: %s", path,
256 rtld_strerror(errno));
262 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
263 (data_vlimit - data_vaddr + data_offset) >=
264 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
265 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
271 obj->dev = sb->st_dev;
272 obj->ino = sb->st_ino;
274 obj->mapbase = mapbase;
275 obj->mapsize = mapsize;
276 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
278 obj->vaddrbase = base_vaddr;
279 obj->relocbase = mapbase - base_vaddr;
280 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
281 if (hdr->e_entry != 0)
282 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
283 if (phdr_vaddr != 0) {
284 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr);
286 obj->phdr = malloc(phsize);
287 if (obj->phdr == NULL) {
289 _rtld_error("%s: cannot allocate program header", path);
292 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize);
293 obj->phdr_alloc = true;
295 obj->phsize = phsize;
296 if (phinterp != NULL)
297 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
299 tls_dtv_generation++;
300 obj->tlsindex = ++tls_max_index;
301 obj->tlssize = phtls->p_memsz;
302 obj->tlsalign = phtls->p_align;
303 obj->tlsinitsize = phtls->p_filesz;
304 obj->tlsinit = mapbase + phtls->p_vaddr;
306 obj->stack_flags = stack_flags;
307 obj->relro_page = obj->relocbase + trunc_page(relro_page);
308 obj->relro_size = round_page(relro_size);
309 if (note_start < note_end)
310 digest_notes(obj, note_start, note_end);
311 if (note_map != NULL)
312 munmap(note_map, note_map_len);
313 munmap(hdr, PAGE_SIZE);
317 munmap(mapbase, mapsize);
319 if (note_map != NULL && note_map != MAP_FAILED)
320 munmap(note_map, note_map_len);
321 munmap(hdr, PAGE_SIZE);
326 get_elf_header(int fd, const char *path)
330 hdr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
332 if (hdr == (Elf_Ehdr *)MAP_FAILED) {
333 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
337 /* Make sure the file is valid */
339 _rtld_error("%s: invalid file format", path);
342 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
343 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
344 _rtld_error("%s: unsupported file layout", path);
347 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
348 hdr->e_version != EV_CURRENT) {
349 _rtld_error("%s: unsupported file version", path);
352 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
353 _rtld_error("%s: unsupported file type", path);
356 if (hdr->e_machine != ELF_TARG_MACH) {
357 _rtld_error("%s: unsupported machine", path);
362 * We rely on the program header being in the first page. This is
363 * not strictly required by the ABI specification, but it seems to
364 * always true in practice. And, it simplifies things considerably.
366 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
368 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
371 if (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) >
373 _rtld_error("%s: program header too large", path);
379 munmap(hdr, PAGE_SIZE);
384 obj_free(Obj_Entry *obj)
389 free_tls_offset(obj);
390 while (obj->needed != NULL) {
391 Needed_Entry *needed = obj->needed;
392 obj->needed = needed->next;
395 while (!STAILQ_EMPTY(&obj->names)) {
396 Name_Entry *entry = STAILQ_FIRST(&obj->names);
397 STAILQ_REMOVE_HEAD(&obj->names, link);
400 while (!STAILQ_EMPTY(&obj->dldags)) {
401 elm = STAILQ_FIRST(&obj->dldags);
402 STAILQ_REMOVE_HEAD(&obj->dldags, link);
405 while (!STAILQ_EMPTY(&obj->dagmembers)) {
406 elm = STAILQ_FIRST(&obj->dagmembers);
407 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
412 if (obj->origin_path)
413 free(obj->origin_path);
421 free((void *)obj->phdr);
430 obj = CNEW(Obj_Entry);
431 STAILQ_INIT(&obj->dldags);
432 STAILQ_INIT(&obj->dagmembers);
433 STAILQ_INIT(&obj->names);
438 * Given a set of ELF protection flags, return the corresponding protection
442 convert_prot(int elfflags)
455 convert_flags(int elfflags)
457 int flags = MAP_PRIVATE; /* All mappings are private */
460 * Readonly mappings are marked "MAP_NOCORE", because they can be
461 * reconstructed by a debugger.
463 if (!(elfflags & PF_W))