2 * SPDX-License-Identifier: BSD-2-Clause
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.
31 #include <sys/param.h>
44 static Elf_Ehdr *get_elf_header(int, const char *, const struct stat *,
46 static int convert_flags(int); /* Elf flags -> mmap flags */
48 int __getosreldate(void);
51 phdr_in_zero_page(const Elf_Ehdr *hdr)
53 return (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) <= page_size);
57 * Map a shared object into memory. The "fd" argument is a file descriptor,
58 * which must be open on the object and positioned at its beginning.
59 * The "path" argument is a pathname that is used only for error messages.
61 * The return value is a pointer to a newly-allocated Obj_Entry structure
62 * for the shared object. Returns NULL on failure.
65 map_object(int fd, const char *path, const struct stat *sb)
93 size_t nclear, phsize;
106 hdr = get_elf_header(fd, path, sb, &phdr);
111 * Scan the program header entries, and save key information.
112 * We expect that the loadable segments are ordered by load address.
114 phsize = hdr->e_phnum * sizeof(phdr[0]);
115 phlimit = phdr + hdr->e_phnum;
117 phdyn = phinterp = phtls = NULL;
125 segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
126 stack_flags = PF_X | PF_R | PF_W;
128 while (phdr < phlimit) {
129 switch (phdr->p_type) {
136 segs[++nsegs] = phdr;
137 if ((segs[nsegs]->p_align & (page_size - 1)) != 0) {
138 _rtld_error("%s: PT_LOAD segment %d not page-aligned",
142 if ((segs[nsegs]->p_flags & PF_X) == PF_X) {
143 text_end = MAX(text_end,
144 rtld_round_page(segs[nsegs]->p_vaddr +
145 segs[nsegs]->p_memsz));
150 phdr_vaddr = phdr->p_vaddr;
151 phsize = phdr->p_memsz;
163 stack_flags = phdr->p_flags;
167 relro_page = phdr->p_vaddr;
168 relro_size = phdr->p_memsz;
172 if (phdr->p_offset > page_size ||
173 phdr->p_offset + phdr->p_filesz > page_size) {
174 note_map_len = rtld_round_page(phdr->p_offset +
175 phdr->p_filesz) - rtld_trunc_page(phdr->p_offset);
176 note_map = mmap(NULL, note_map_len, PROT_READ,
177 MAP_PRIVATE, fd, rtld_trunc_page(phdr->p_offset));
178 if (note_map == MAP_FAILED) {
179 _rtld_error("%s: error mapping PT_NOTE (%d)", path, errno);
182 note_start = (Elf_Addr)(note_map + phdr->p_offset -
183 rtld_trunc_page(phdr->p_offset));
185 note_start = (Elf_Addr)(char *)hdr + phdr->p_offset;
187 note_end = note_start + phdr->p_filesz;
194 _rtld_error("%s: object is not dynamically-linked", path);
199 _rtld_error("%s: too few PT_LOAD segments", path);
204 * Map the entire address space of the object, to stake out our
205 * contiguous region, and to establish the base address for relocation.
207 base_vaddr = rtld_trunc_page(segs[0]->p_vaddr);
208 base_vlimit = rtld_round_page(segs[nsegs]->p_vaddr + segs[nsegs]->p_memsz);
209 mapsize = base_vlimit - base_vaddr;
210 base_addr = (caddr_t) base_vaddr;
211 base_flags = __getosreldate() >= P_OSREL_MAP_GUARD ? MAP_GUARD :
212 MAP_PRIVATE | MAP_ANON | MAP_NOCORE;
213 if (npagesizes > 1 && rtld_round_page(segs[0]->p_filesz) >= pagesizes[1])
214 base_flags |= MAP_ALIGNED_SUPER;
216 base_flags |= MAP_FIXED | MAP_EXCL;
218 mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
219 if (mapbase == MAP_FAILED) {
220 _rtld_error("%s: mmap of entire address space failed: %s",
221 path, rtld_strerror(errno));
224 if (base_addr != NULL && mapbase != base_addr) {
225 _rtld_error("%s: mmap returned wrong address: wanted %p, got %p",
226 path, base_addr, mapbase);
230 for (i = 0; i <= nsegs; i++) {
231 /* Overlay the segment onto the proper region. */
232 data_offset = rtld_trunc_page(segs[i]->p_offset);
233 data_vaddr = rtld_trunc_page(segs[i]->p_vaddr);
234 data_vlimit = rtld_round_page(segs[i]->p_vaddr + segs[i]->p_filesz);
235 data_addr = mapbase + (data_vaddr - base_vaddr);
236 data_prot = convert_prot(segs[i]->p_flags);
237 data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
238 if (data_vlimit != data_vaddr &&
239 mmap(data_addr, data_vlimit - data_vaddr, data_prot,
240 data_flags | MAP_PREFAULT_READ, fd, data_offset) == MAP_FAILED) {
241 _rtld_error("%s: mmap of data failed: %s", path,
242 rtld_strerror(errno));
247 if (segs[i]->p_filesz != segs[i]->p_memsz) {
249 /* Clear any BSS in the last page of the segment. */
250 clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
251 clear_addr = mapbase + (clear_vaddr - base_vaddr);
252 clear_page = mapbase + (rtld_trunc_page(clear_vaddr) - base_vaddr);
254 if ((nclear = data_vlimit - clear_vaddr) > 0) {
255 /* Make sure the end of the segment is writable */
256 if ((data_prot & PROT_WRITE) == 0 && -1 ==
257 mprotect(clear_page, page_size, data_prot|PROT_WRITE)) {
258 _rtld_error("%s: mprotect failed: %s", path,
259 rtld_strerror(errno));
263 memset(clear_addr, 0, nclear);
265 /* Reset the data protection back */
266 if ((data_prot & PROT_WRITE) == 0)
267 mprotect(clear_page, page_size, data_prot);
270 /* Overlay the BSS segment onto the proper region. */
271 bss_vaddr = data_vlimit;
272 bss_vlimit = rtld_round_page(segs[i]->p_vaddr + segs[i]->p_memsz);
273 bss_addr = mapbase + (bss_vaddr - base_vaddr);
274 if (bss_vlimit > bss_vaddr) { /* There is something to do */
275 if (mmap(bss_addr, bss_vlimit - bss_vaddr, data_prot,
276 data_flags | MAP_ANON, -1, 0) == MAP_FAILED) {
277 _rtld_error("%s: mmap of bss failed: %s", path,
278 rtld_strerror(errno));
284 if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
285 (data_vlimit - data_vaddr + data_offset) >=
286 (hdr->e_phoff + hdr->e_phnum * sizeof (Elf_Phdr))) {
287 phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
293 obj->dev = sb->st_dev;
294 obj->ino = sb->st_ino;
296 obj->mapbase = mapbase;
297 obj->mapsize = mapsize;
298 obj->vaddrbase = base_vaddr;
299 obj->relocbase = mapbase - base_vaddr;
300 obj->dynamic = (const Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr);
301 if (hdr->e_entry != 0)
302 obj->entry = (caddr_t)(obj->relocbase + hdr->e_entry);
303 if (phdr_vaddr != 0) {
304 obj->phdr = (const Elf_Phdr *)(obj->relocbase + phdr_vaddr);
306 obj->phdr = malloc(phsize);
307 if (obj->phdr == NULL) {
309 _rtld_error("%s: cannot allocate program header", path);
312 memcpy(__DECONST(char *, obj->phdr), (char *)hdr + hdr->e_phoff, phsize);
313 obj->phdr_alloc = true;
315 obj->phsize = phsize;
316 if (phinterp != NULL)
317 obj->interp = (const char *)(obj->relocbase + phinterp->p_vaddr);
319 tls_dtv_generation++;
320 obj->tlsindex = ++tls_max_index;
321 obj->tlssize = phtls->p_memsz;
322 obj->tlsalign = phtls->p_align;
323 obj->tlspoffset = phtls->p_offset;
324 obj->tlsinitsize = phtls->p_filesz;
325 obj->tlsinit = mapbase + phtls->p_vaddr;
327 obj->stack_flags = stack_flags;
328 obj->relro_page = obj->relocbase + rtld_trunc_page(relro_page);
329 obj->relro_size = rtld_trunc_page(relro_page + relro_size) -
330 rtld_trunc_page(relro_page);
331 if (note_start < note_end)
332 digest_notes(obj, note_start, note_end);
333 if (note_map != NULL)
334 munmap(note_map, note_map_len);
335 munmap(hdr, page_size);
339 munmap(mapbase, mapsize);
341 if (note_map != NULL && note_map != MAP_FAILED)
342 munmap(note_map, note_map_len);
343 if (!phdr_in_zero_page(hdr))
344 munmap(phdr, hdr->e_phnum * sizeof(phdr[0]));
345 munmap(hdr, page_size);
350 check_elf_headers(const Elf_Ehdr *hdr, const char *path)
353 _rtld_error("%s: invalid file format", path);
356 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
357 hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
358 _rtld_error("%s: unsupported file layout", path);
361 if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
362 hdr->e_version != EV_CURRENT) {
363 _rtld_error("%s: unsupported file version", path);
366 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
367 _rtld_error("%s: unsupported file type", path);
370 if (hdr->e_machine != ELF_TARG_MACH) {
371 _rtld_error("%s: unsupported machine", path);
374 if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
376 "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)", path);
383 get_elf_header(int fd, const char *path, const struct stat *sbp,
389 /* Make sure file has enough data for the ELF header */
390 if (sbp != NULL && sbp->st_size < (off_t)sizeof(Elf_Ehdr)) {
391 _rtld_error("%s: invalid file format", path);
395 hdr = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
397 if (hdr == MAP_FAILED) {
398 _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
402 /* Make sure the file is valid */
403 if (!check_elf_headers(hdr, path))
407 * We rely on the program header being in the first page. This is
408 * not strictly required by the ABI specification, but it seems to
409 * always true in practice. And, it simplifies things considerably.
411 if (phdr_in_zero_page(hdr)) {
412 phdr = (Elf_Phdr *)((char *)hdr + hdr->e_phoff);
414 phdr = mmap(NULL, hdr->e_phnum * sizeof(phdr[0]),
415 PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ, fd,
417 if (phdr == MAP_FAILED) {
418 _rtld_error("%s: error mapping phdr: %s", path,
419 rtld_strerror(errno));
427 munmap(hdr, page_size);
432 obj_free(Obj_Entry *obj)
437 free_tls_offset(obj);
438 while (obj->needed != NULL) {
439 Needed_Entry *needed = obj->needed;
440 obj->needed = needed->next;
443 while (!STAILQ_EMPTY(&obj->names)) {
444 Name_Entry *entry = STAILQ_FIRST(&obj->names);
445 STAILQ_REMOVE_HEAD(&obj->names, link);
448 while (!STAILQ_EMPTY(&obj->dldags)) {
449 elm = STAILQ_FIRST(&obj->dldags);
450 STAILQ_REMOVE_HEAD(&obj->dldags, link);
453 while (!STAILQ_EMPTY(&obj->dagmembers)) {
454 elm = STAILQ_FIRST(&obj->dagmembers);
455 STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
460 if (obj->origin_path)
461 free(obj->origin_path);
463 free(__DECONST(void*, obj->rpath));
469 free(__DECONST(void *, obj->phdr));
478 obj = CNEW(Obj_Entry);
479 STAILQ_INIT(&obj->dldags);
480 STAILQ_INIT(&obj->dagmembers);
481 STAILQ_INIT(&obj->names);
486 * Given a set of ELF protection flags, return the corresponding protection
490 convert_prot(int elfflags)
503 convert_flags(int elfflags)
505 int flags = MAP_PRIVATE; /* All mappings are private */
508 * Readonly mappings are marked "MAP_NOCORE", because they can be
509 * reconstructed by a debugger.
511 if (!(elfflags & PF_W))