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 *, const struct stat *);
42 static int convert_flags(int); /* Elf flags -> mmap flags */
44 int __getosreldate(void);
47 * Map a shared object into memory. The "fd" argument is a file descriptor,
48 * which must be open on the object and positioned at its beginning.
49 * The "path" argument is a pathname that is used only for error messages.
51 * The return value is a pointer to a newly-allocated Obj_Entry structure
52 * for the shared object. Returns NULL on failure.
55 map_object(int fd, const char *path, const struct stat *sb)
83 size_t nclear, phsize;
95 hdr = get_elf_header(fd, path, sb);
100 * Scan the program header entries, and save key information.
102 * We expect that the loadable segments are ordered by load address.
104 phdr = (Elf_Phdr *) ((char *)hdr + hdr->e_phoff);
105 phsize = hdr->e_phnum * sizeof (phdr[0]);
106 phlimit = phdr + hdr->e_phnum;
108 phdyn = phinterp = phtls = NULL;
115 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
116 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
117 while (phdr < phlimit) {
118 switch (phdr->p_type) {
125 segs[++nsegs] = phdr;
126 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
127 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
134 phdr_vaddr = phdr->p_vaddr;
135 phsize = phdr->p_memsz;
147 stack_flags = phdr->p_flags;
151 relro_page = phdr->p_vaddr;
152 relro_size = phdr->p_memsz;
156 if (phdr->p_offset > PAGE_SIZE ||
157 phdr->p_offset + phdr->p_filesz > PAGE_SIZE) {
158 note_map_len = round_page(phdr->p_offset +
159 phdr->p_filesz) - trunc_page(phdr->p_offset);
160 note_map = mmap(NULL, note_map_len, PROT_READ,
161 MAP_PRIVATE, fd, trunc_page(phdr->p_offset));
162 if (note_map == MAP_FAILED) {
163 _rtld_error("%s: error mapping PT_NOTE (%d)", path, errno);
166 note_start = (Elf_Addr)(note_map + phdr->p_offset -
167 trunc_page(phdr->p_offset));
169 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
171 note_end = note_start + phdr->p_filesz;
178 _rtld_error("%s: object is not dynamically-linked", path);
183 _rtld_error("%s: too few PT_LOAD segments", path);
188 * Map the entire address space of the object, to stake out our
189 * contiguous region, and to establish the base address for relocation.
191 base_vaddr = trunc_page(segs[0]->p_vaddr);
192 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
193 mapsize = base_vlimit - base_vaddr;
194 base_addr = (caddr_t) base_vaddr;
195 base_flags = __getosreldate() >= P_OSREL_MAP_GUARD ? MAP_GUARD :
196 MAP_PRIVATE | MAP_ANON | MAP_NOCORE;
197 if (npagesizes > 1 && round_page(segs[0]->p_filesz) >= pagesizes[1])
198 base_flags |= MAP_ALIGNED_SUPER;
200 base_flags |= MAP_FIXED | MAP_EXCL;
202 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
203 if (mapbase == (caddr_t) -1) {
204 _rtld_error("%s: mmap of entire address space failed: %s",
205 path, rtld_strerror(errno));
208 if (base_addr != NULL && mapbase != base_addr) {
209 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
210 path, base_addr, mapbase);
214 for (i = 0; i <= nsegs; i++) {
215 /* Overlay the segment onto the proper region. */
216 data_offset = trunc_page(segs[i]->p_offset);
217 data_vaddr = trunc_page(segs[i]->p_vaddr);
218 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
219 data_addr = mapbase + (data_vaddr - base_vaddr);
220 data_prot = convert_prot(segs[i]->p_flags);
221 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
222 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
223 data_flags | MAP_PREFAULT_READ, fd, data_offset) == (caddr_t) -1) {
224 _rtld_error("%s: mmap of data failed: %s", path,
225 rtld_strerror(errno));
230 if (segs[i]->p_filesz != segs[i]->p_memsz) {
232 /* Clear any BSS in the last page of the segment. */
233 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
234 clear_addr = mapbase + (clear_vaddr - base_vaddr);
235 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
237 if ((nclear = data_vlimit - clear_vaddr) > 0) {
238 /* Make sure the end of the segment is writable */
239 if ((data_prot & PROT_WRITE) == 0 && -1 ==
240 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
241 _rtld_error("%s: mprotect failed: %s", path,
242 rtld_strerror(errno));
246 memset(clear_addr, 0, nclear);
248 /* Reset the data protection back */
249 if ((data_prot & PROT_WRITE) == 0)
250 mprotect(clear_page, PAGE_SIZE, data_prot);
253 /* Overlay the BSS segment onto the proper region. */
254 bss_vaddr = data_vlimit;
255 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
256 bss_addr = mapbase + (bss_vaddr - base_vaddr);
257 if (bss_vlimit > bss_vaddr) { /* There is something to do */
258 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
259 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
260 _rtld_error("%s: mmap of bss failed: %s", path,
261 rtld_strerror(errno));
267 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
268 (data_vlimit - data_vaddr + data_offset) >=
269 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
270 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
276 obj->dev = sb->st_dev;
277 obj->ino = sb->st_ino;
279 obj->mapbase = mapbase;
280 obj->mapsize = mapsize;
281 obj->textsize = round_page(segs[0]->p_vaddr + segs[0]->p_memsz) -
283 obj->vaddrbase = base_vaddr;
284 obj->relocbase = mapbase - base_vaddr;
285 obj->dynamic = (const Elf_Dyn *) (obj->relocbase + phdyn->p_vaddr);
286 if (hdr->e_entry != 0)
287 obj->entry = (caddr_t) (obj->relocbase + hdr->e_entry);
288 if (phdr_vaddr != 0) {
289 obj->phdr = (const Elf_Phdr *) (obj->relocbase + phdr_vaddr);
291 obj->phdr = malloc(phsize);
292 if (obj->phdr == NULL) {
294 _rtld_error("%s: cannot allocate program header", path);
297 memcpy((char *)obj->phdr, (char *)hdr + hdr->e_phoff, phsize);
298 obj->phdr_alloc = true;
300 obj->phsize = phsize;
301 if (phinterp != NULL)
302 obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
304 tls_dtv_generation++;
305 obj->tlsindex = ++tls_max_index;
306 obj->tlssize = phtls->p_memsz;
307 obj->tlsalign = phtls->p_align;
308 obj->tlsinitsize = phtls->p_filesz;
309 obj->tlsinit = mapbase + phtls->p_vaddr;
311 obj->stack_flags = stack_flags;
312 obj->relro_page = obj->relocbase + trunc_page(relro_page);
313 obj->relro_size = round_page(relro_size);
314 if (note_start < note_end)
315 digest_notes(obj, note_start, note_end);
316 if (note_map != NULL)
317 munmap(note_map, note_map_len);
318 munmap(hdr, PAGE_SIZE);
322 munmap(mapbase, mapsize);
324 if (note_map != NULL && note_map != MAP_FAILED)
325 munmap(note_map, note_map_len);
326 munmap(hdr, PAGE_SIZE);
331 get_elf_header(int fd, const char *path, const struct stat *sbp)
335 /* Make sure file has enough data for the ELF header */
336 if (sbp != NULL && sbp->st_size < sizeof(Elf_Ehdr)) {
337 _rtld_error("%s: invalid file format", path);
341 hdr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
343 if (hdr == (Elf_Ehdr *)MAP_FAILED) {
344 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
348 /* Make sure the file is valid */
350 _rtld_error("%s: invalid file format", path);
353 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
354 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
355 _rtld_error("%s: unsupported file layout", path);
358 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
359 hdr->e_version != EV_CURRENT) {
360 _rtld_error("%s: unsupported file version", path);
363 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
364 _rtld_error("%s: unsupported file type", path);
367 if (hdr->e_machine != ELF_TARG_MACH) {
368 _rtld_error("%s: unsupported machine", path);
373 * We rely on the program header being in the first page. This is
374 * not strictly required by the ABI specification, but it seems to
375 * always true in practice. And, it simplifies things considerably.
377 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
379 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
382 if (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) >
384 _rtld_error("%s: program header too large", path);
390 munmap(hdr, PAGE_SIZE);
395 obj_free(Obj_Entry *obj)
400 free_tls_offset(obj);
401 while (obj->needed != NULL) {
402 Needed_Entry *needed = obj->needed;
403 obj->needed = needed->next;
406 while (!STAILQ_EMPTY(&obj->names)) {
407 Name_Entry *entry = STAILQ_FIRST(&obj->names);
408 STAILQ_REMOVE_HEAD(&obj->names, link);
411 while (!STAILQ_EMPTY(&obj->dldags)) {
412 elm = STAILQ_FIRST(&obj->dldags);
413 STAILQ_REMOVE_HEAD(&obj->dldags, link);
416 while (!STAILQ_EMPTY(&obj->dagmembers)) {
417 elm = STAILQ_FIRST(&obj->dagmembers);
418 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
423 if (obj->origin_path)
424 free(obj->origin_path);
432 free((void *)obj->phdr);
441 obj = CNEW(Obj_Entry);
442 STAILQ_INIT(&obj->dldags);
443 STAILQ_INIT(&obj->dagmembers);
444 STAILQ_INIT(&obj->names);
449 * Given a set of ELF protection flags, return the corresponding protection
453 convert_prot(int elfflags)
466 convert_flags(int elfflags)
468 int flags = MAP_PRIVATE; /* All mappings are private */
471 * Readonly mappings are marked "MAP_NOCORE", because they can be
472 * reconstructed by a debugger.
474 if (!(elfflags & PF_W))