2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 1996-1998 John D. Polstra.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
43 static Elf_Ehdr *get_elf_header(int, const char *, const struct stat *);
44 static int convert_flags(int); /* Elf flags -> mmap flags */
46 int __getosreldate(void);
49 * Map a shared object into memory. The "fd" argument is a file descriptor,
50 * which must be open on the object and positioned at its beginning.
51 * The "path" argument is a pathname that is used only for error messages.
53 * The return value is a pointer to a newly-allocated Obj_Entry structure
54 * for the shared object. Returns NULL on failure.
57 map_object(int fd, const char *path, const struct stat *sb)
85 size_t nclear, phsize;
98 hdr = get_elf_header(fd, path, sb);
103 * Scan the program header entries, and save key information.
105 * We expect that the loadable segments are ordered by load address.
107 phdr = (Elf_Phdr *)((char *)hdr + hdr->e_phoff);
108 phsize = hdr->e_phnum * sizeof (phdr[0]);
109 phlimit = phdr + hdr->e_phnum;
111 phdyn = phinterp = phtls = NULL;
119 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
120 stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
122 while (phdr < phlimit) {
123 switch (phdr->p_type) {
130 segs[++nsegs] = phdr;
131 if ((segs[nsegs]->p_align & (PAGE_SIZE - 1)) != 0) {
132 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
136 if ((segs[nsegs]->p_flags & PF_X) == PF_X) {
137 text_end = MAX(text_end,
138 round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz));
143 phdr_vaddr = phdr->p_vaddr;
144 phsize = phdr->p_memsz;
156 stack_flags = phdr->p_flags;
160 relro_page = phdr->p_vaddr;
161 relro_size = phdr->p_memsz;
165 if (phdr->p_offset > PAGE_SIZE ||
166 phdr->p_offset + phdr->p_filesz > PAGE_SIZE) {
167 note_map_len = round_page(phdr->p_offset +
168 phdr->p_filesz) - trunc_page(phdr->p_offset);
169 note_map = mmap(NULL, note_map_len, PROT_READ,
170 MAP_PRIVATE, fd, trunc_page(phdr->p_offset));
171 if (note_map == MAP_FAILED) {
172 _rtld_error("%s: error mapping PT_NOTE (%d)", path, errno);
175 note_start = (Elf_Addr)(note_map + phdr->p_offset -
176 trunc_page(phdr->p_offset));
178 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
180 note_end = note_start + phdr->p_filesz;
187 _rtld_error("%s: object is not dynamically-linked", path);
192 _rtld_error("%s: too few PT_LOAD segments", path);
197 * Map the entire address space of the object, to stake out our
198 * contiguous region, and to establish the base address for relocation.
200 base_vaddr = trunc_page(segs[0]->p_vaddr);
201 base_vlimit = round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
202 mapsize = base_vlimit - base_vaddr;
203 base_addr = (caddr_t) base_vaddr;
204 base_flags = __getosreldate() >= P_OSREL_MAP_GUARD ? MAP_GUARD :
205 MAP_PRIVATE | MAP_ANON | MAP_NOCORE;
206 if (npagesizes > 1 && round_page(segs[0]->p_filesz) >= pagesizes[1])
207 base_flags |= MAP_ALIGNED_SUPER;
209 base_flags |= MAP_FIXED | MAP_EXCL;
211 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
212 if (mapbase == (caddr_t) -1) {
213 _rtld_error("%s: mmap of entire address space failed: %s",
214 path, rtld_strerror(errno));
217 if (base_addr != NULL && mapbase != base_addr) {
218 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
219 path, base_addr, mapbase);
223 for (i = 0; i <= nsegs; i++) {
224 /* Overlay the segment onto the proper region. */
225 data_offset = trunc_page(segs[i]->p_offset);
226 data_vaddr = trunc_page(segs[i]->p_vaddr);
227 data_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
228 data_addr = mapbase + (data_vaddr - base_vaddr);
229 data_prot = convert_prot(segs[i]->p_flags);
230 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
231 if (mmap(data_addr, data_vlimit - data_vaddr, data_prot,
232 data_flags | MAP_PREFAULT_READ, fd, data_offset) == (caddr_t) -1) {
233 _rtld_error("%s: mmap of data failed: %s", path,
234 rtld_strerror(errno));
239 if (segs[i]->p_filesz != segs[i]->p_memsz) {
241 /* Clear any BSS in the last page of the segment. */
242 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
243 clear_addr = mapbase + (clear_vaddr - base_vaddr);
244 clear_page = mapbase + (trunc_page(clear_vaddr) - base_vaddr);
246 if ((nclear = data_vlimit - clear_vaddr) > 0) {
247 /* Make sure the end of the segment is writable */
248 if ((data_prot & PROT_WRITE) == 0 && -1 ==
249 mprotect(clear_page, PAGE_SIZE, data_prot|PROT_WRITE)) {
250 _rtld_error("%s: mprotect failed: %s", path,
251 rtld_strerror(errno));
255 memset(clear_addr, 0, nclear);
257 /* Reset the data protection back */
258 if ((data_prot & PROT_WRITE) == 0)
259 mprotect(clear_page, PAGE_SIZE, data_prot);
262 /* Overlay the BSS segment onto the proper region. */
263 bss_vaddr = data_vlimit;
264 bss_vlimit = round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
265 bss_addr = mapbase + (bss_vaddr - base_vaddr);
266 if (bss_vlimit > bss_vaddr) { /* There is something to do */
267 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
268 data_flags | MAP_ANON, -1, 0) == (caddr_t)-1) {
269 _rtld_error("%s: mmap of bss failed: %s", path,
270 rtld_strerror(errno));
276 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
277 (data_vlimit - data_vaddr + data_offset) >=
278 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
279 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
285 obj->dev = sb->st_dev;
286 obj->ino = sb->st_ino;
288 obj->mapbase = mapbase;
289 obj->mapsize = mapsize;
290 obj->vaddrbase = base_vaddr;
291 obj->relocbase = mapbase - base_vaddr;
292 obj->dynamic = (const Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr);
293 if (hdr->e_entry != 0)
294 obj->entry = (caddr_t)(obj->relocbase + hdr->e_entry);
295 if (phdr_vaddr != 0) {
296 obj->phdr = (const Elf_Phdr *)(obj->relocbase + phdr_vaddr);
298 obj->phdr = malloc(phsize);
299 if (obj->phdr == NULL) {
301 _rtld_error("%s: cannot allocate program header", path);
304 memcpy(__DECONST(char *, obj->phdr), (char *)hdr + hdr->e_phoff, phsize);
305 obj->phdr_alloc = true;
307 obj->phsize = phsize;
308 if (phinterp != NULL)
309 obj->interp = (const char *)(obj->relocbase + phinterp->p_vaddr);
311 tls_dtv_generation++;
312 obj->tlsindex = ++tls_max_index;
313 obj->tlssize = phtls->p_memsz;
314 obj->tlsalign = phtls->p_align;
315 obj->tlsinitsize = phtls->p_filesz;
316 obj->tlsinit = mapbase + phtls->p_vaddr;
318 obj->stack_flags = stack_flags;
319 obj->relro_page = obj->relocbase + trunc_page(relro_page);
320 obj->relro_size = round_page(relro_size);
321 if (note_start < note_end)
322 digest_notes(obj, note_start, note_end);
323 if (note_map != NULL)
324 munmap(note_map, note_map_len);
325 munmap(hdr, PAGE_SIZE);
329 munmap(mapbase, mapsize);
331 if (note_map != NULL && note_map != MAP_FAILED)
332 munmap(note_map, note_map_len);
333 munmap(hdr, PAGE_SIZE);
338 get_elf_header(int fd, const char *path, const struct stat *sbp)
342 /* Make sure file has enough data for the ELF header */
343 if (sbp != NULL && sbp->st_size < (off_t)sizeof(Elf_Ehdr)) {
344 _rtld_error("%s: invalid file format", path);
348 hdr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
350 if (hdr == (Elf_Ehdr *)MAP_FAILED) {
351 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
355 /* Make sure the file is valid */
357 _rtld_error("%s: invalid file format", path);
360 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
361 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
362 _rtld_error("%s: unsupported file layout", path);
365 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
366 hdr->e_version != EV_CURRENT) {
367 _rtld_error("%s: unsupported file version", path);
370 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
371 _rtld_error("%s: unsupported file type", path);
374 if (hdr->e_machine != ELF_TARG_MACH) {
375 _rtld_error("%s: unsupported machine", path);
380 * We rely on the program header being in the first page. This is
381 * not strictly required by the ABI specification, but it seems to
382 * always true in practice. And, it simplifies things considerably.
384 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
386 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
389 if (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) >
391 _rtld_error("%s: program header too large", path);
397 munmap(hdr, PAGE_SIZE);
402 obj_free(Obj_Entry *obj)
407 free_tls_offset(obj);
408 while (obj->needed != NULL) {
409 Needed_Entry *needed = obj->needed;
410 obj->needed = needed->next;
413 while (!STAILQ_EMPTY(&obj->names)) {
414 Name_Entry *entry = STAILQ_FIRST(&obj->names);
415 STAILQ_REMOVE_HEAD(&obj->names, link);
418 while (!STAILQ_EMPTY(&obj->dldags)) {
419 elm = STAILQ_FIRST(&obj->dldags);
420 STAILQ_REMOVE_HEAD(&obj->dldags, link);
423 while (!STAILQ_EMPTY(&obj->dagmembers)) {
424 elm = STAILQ_FIRST(&obj->dagmembers);
425 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
430 if (obj->origin_path)
431 free(obj->origin_path);
433 free(__DECONST(void*, obj->rpath));
439 free(__DECONST(void *, obj->phdr));
448 obj = CNEW(Obj_Entry);
449 STAILQ_INIT(&obj->dldags);
450 STAILQ_INIT(&obj->dagmembers);
451 STAILQ_INIT(&obj->names);
456 * Given a set of ELF protection flags, return the corresponding protection
460 convert_prot(int elfflags)
473 convert_flags(int elfflags)
475 int flags = MAP_PRIVATE; /* All mappings are private */
478 * Readonly mappings are marked "MAP_NOCORE", because they can be
479 * reconstructed by a debugger.
481 if (!(elfflags & PF_W))