13124c3e0SJohn Polstra /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3e6209940SPedro F. Giffuni *
43124c3e0SJohn Polstra * Copyright 1996-1998 John D. Polstra.
53124c3e0SJohn Polstra * All rights reserved.
63124c3e0SJohn Polstra *
73124c3e0SJohn Polstra * Redistribution and use in source and binary forms, with or without
83124c3e0SJohn Polstra * modification, are permitted provided that the following conditions
93124c3e0SJohn Polstra * are met:
103124c3e0SJohn Polstra * 1. Redistributions of source code must retain the above copyright
113124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer.
123124c3e0SJohn Polstra * 2. Redistributions in binary form must reproduce the above copyright
133124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer in the
143124c3e0SJohn Polstra * documentation and/or other materials provided with the distribution.
153124c3e0SJohn Polstra *
163124c3e0SJohn Polstra * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173124c3e0SJohn Polstra * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183124c3e0SJohn Polstra * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193124c3e0SJohn Polstra * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203124c3e0SJohn Polstra * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
213124c3e0SJohn Polstra * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223124c3e0SJohn Polstra * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
233124c3e0SJohn Polstra * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
243124c3e0SJohn Polstra * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
253124c3e0SJohn Polstra * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
263124c3e0SJohn Polstra */
273124c3e0SJohn Polstra
285942b4b6SKonstantin Belousov #define _WANT_P_OSREL
293124c3e0SJohn Polstra #include <sys/param.h>
303124c3e0SJohn Polstra #include <sys/mman.h>
317360ae0fSJohn Polstra #include <sys/stat.h>
323124c3e0SJohn Polstra
333124c3e0SJohn Polstra #include <errno.h>
343124c3e0SJohn Polstra #include <stddef.h>
35926ea445SJohn Polstra #include <stdlib.h>
363124c3e0SJohn Polstra #include <string.h>
373124c3e0SJohn Polstra #include <unistd.h>
383124c3e0SJohn Polstra
39b5393d9fSDoug Rabson #include "debug.h"
403124c3e0SJohn Polstra #include "rtld.h"
413124c3e0SJohn Polstra
425d6d106cSKonstantin Belousov static Elf_Ehdr *get_elf_header(int, const char *, const struct stat *,
435d6d106cSKonstantin Belousov Elf_Phdr **phdr);
44fa7dd9c5SMatthew Dillon static int convert_flags(int); /* Elf flags -> mmap flags */
453124c3e0SJohn Polstra
46a36deee3SKonstantin Belousov int __getosreldate(void);
47a36deee3SKonstantin Belousov
485d6d106cSKonstantin Belousov static bool
phdr_in_zero_page(const Elf_Ehdr * hdr)495d6d106cSKonstantin Belousov phdr_in_zero_page(const Elf_Ehdr *hdr)
505d6d106cSKonstantin Belousov {
51e85eaa93SAndrew Turner return (hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr) <= page_size);
525d6d106cSKonstantin Belousov }
535d6d106cSKonstantin Belousov
543124c3e0SJohn Polstra /*
55bfb1ef60SJohn Polstra * Map a shared object into memory. The "fd" argument is a file descriptor,
563124c3e0SJohn Polstra * which must be open on the object and positioned at its beginning.
57bfb1ef60SJohn Polstra * The "path" argument is a pathname that is used only for error messages.
583124c3e0SJohn Polstra *
593124c3e0SJohn Polstra * The return value is a pointer to a newly-allocated Obj_Entry structure
603124c3e0SJohn Polstra * for the shared object. Returns NULL on failure.
613124c3e0SJohn Polstra */
623124c3e0SJohn Polstra Obj_Entry *
map_object(int fd,const char * path,const struct stat * sb)637360ae0fSJohn Polstra map_object(int fd, const char *path, const struct stat *sb)
643124c3e0SJohn Polstra {
653124c3e0SJohn Polstra Obj_Entry *obj;
66341b3de6SMatthew N. Dodd Elf_Ehdr *hdr;
6778af18bdSDavid E. O'Brien int i;
6813575fc4SDoug Rabson Elf_Phdr *phdr;
6913575fc4SDoug Rabson Elf_Phdr *phlimit;
708b7f25d4SAlexander Kabaev Elf_Phdr **segs;
713124c3e0SJohn Polstra int nsegs;
7213575fc4SDoug Rabson Elf_Phdr *phdyn;
73a607e5d7SJohn Polstra Elf_Phdr *phinterp;
74017246d0SDoug Rabson Elf_Phdr *phtls;
753124c3e0SJohn Polstra caddr_t mapbase;
763124c3e0SJohn Polstra size_t mapsize;
7713575fc4SDoug Rabson Elf_Addr base_vaddr;
7813575fc4SDoug Rabson Elf_Addr base_vlimit;
793124c3e0SJohn Polstra caddr_t base_addr;
80ea8577c7SAlan Cox int base_flags;
8113575fc4SDoug Rabson Elf_Off data_offset;
8213575fc4SDoug Rabson Elf_Addr data_vaddr;
8313575fc4SDoug Rabson Elf_Addr data_vlimit;
843124c3e0SJohn Polstra caddr_t data_addr;
858b7f25d4SAlexander Kabaev int data_prot;
86fa7dd9c5SMatthew Dillon int data_flags;
8713575fc4SDoug Rabson Elf_Addr clear_vaddr;
883124c3e0SJohn Polstra caddr_t clear_addr;
898b7f25d4SAlexander Kabaev caddr_t clear_page;
9049f90ad2SAlexander Kabaev Elf_Addr phdr_vaddr;
9149f90ad2SAlexander Kabaev size_t nclear, phsize;
9213575fc4SDoug Rabson Elf_Addr bss_vaddr;
9313575fc4SDoug Rabson Elf_Addr bss_vlimit;
943124c3e0SJohn Polstra caddr_t bss_addr;
95212f264cSKonstantin Belousov Elf_Word stack_flags;
9683aa9cc0SKonstantin Belousov Elf_Addr note_start;
9783aa9cc0SKonstantin Belousov Elf_Addr note_end;
98bd56d410SKonstantin Belousov char *note_map;
99bd56d410SKonstantin Belousov size_t note_map_len;
100ca7e27bbSAlex Richardson Elf_Addr text_end;
1013124c3e0SJohn Polstra
1025d6d106cSKonstantin Belousov hdr = get_elf_header(fd, path, sb, &phdr);
103341b3de6SMatthew N. Dodd if (hdr == NULL)
104341b3de6SMatthew N. Dodd return (NULL);
1053124c3e0SJohn Polstra
1063124c3e0SJohn Polstra /*
1073124c3e0SJohn Polstra * Scan the program header entries, and save key information.
10811e0093fSKonstantin Belousov * We expect that the loadable segments are ordered by load address.
1093124c3e0SJohn Polstra */
11049f90ad2SAlexander Kabaev phsize = hdr->e_phnum * sizeof(phdr[0]);
111341b3de6SMatthew N. Dodd phlimit = phdr + hdr->e_phnum;
1128b7f25d4SAlexander Kabaev nsegs = -1;
11349f90ad2SAlexander Kabaev phdyn = phinterp = phtls = NULL;
11449f90ad2SAlexander Kabaev phdr_vaddr = 0;
11583aa9cc0SKonstantin Belousov note_start = 0;
11683aa9cc0SKonstantin Belousov note_end = 0;
117bd56d410SKonstantin Belousov note_map = NULL;
1183ab5b6bdSAlex Richardson note_map_len = 0;
119341b3de6SMatthew N. Dodd segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
120b069d3e0SJohn Baldwin stack_flags = PF_X | PF_R | PF_W;
121ca7e27bbSAlex Richardson text_end = 0;
1223124c3e0SJohn Polstra while (phdr < phlimit) {
1233124c3e0SJohn Polstra switch (phdr->p_type) {
124a607e5d7SJohn Polstra case PT_INTERP:
125a607e5d7SJohn Polstra phinterp = phdr;
126a607e5d7SJohn Polstra break;
127a607e5d7SJohn Polstra
1283124c3e0SJohn Polstra case PT_LOAD:
1298b7f25d4SAlexander Kabaev segs[++nsegs] = phdr;
130e85eaa93SAndrew Turner if ((segs[nsegs]->p_align & (page_size - 1)) != 0) {
131*986c96b5SKonstantin Belousov _rtld_error(
132*986c96b5SKonstantin Belousov "%s: PT_LOAD segment %d not page-aligned",
1338b7f25d4SAlexander Kabaev path, nsegs);
134e474e51eSKonstantin Belousov goto error;
135bfb1ef60SJohn Polstra }
136ca7e27bbSAlex Richardson if ((segs[nsegs]->p_flags & PF_X) == PF_X) {
137ca7e27bbSAlex Richardson text_end = MAX(text_end,
138e85eaa93SAndrew Turner rtld_round_page(segs[nsegs]->p_vaddr +
139e85eaa93SAndrew Turner segs[nsegs]->p_memsz));
140ca7e27bbSAlex Richardson }
1413124c3e0SJohn Polstra break;
1423124c3e0SJohn Polstra
1433124c3e0SJohn Polstra case PT_PHDR:
14449f90ad2SAlexander Kabaev phdr_vaddr = phdr->p_vaddr;
14549f90ad2SAlexander Kabaev phsize = phdr->p_memsz;
1463124c3e0SJohn Polstra break;
1473124c3e0SJohn Polstra
1483124c3e0SJohn Polstra case PT_DYNAMIC:
1493124c3e0SJohn Polstra phdyn = phdr;
1503124c3e0SJohn Polstra break;
151017246d0SDoug Rabson
152017246d0SDoug Rabson case PT_TLS:
153017246d0SDoug Rabson phtls = phdr;
154017246d0SDoug Rabson break;
155212f264cSKonstantin Belousov
156212f264cSKonstantin Belousov case PT_GNU_STACK:
157212f264cSKonstantin Belousov stack_flags = phdr->p_flags;
158212f264cSKonstantin Belousov break;
1596d7610d7SKonstantin Belousov
16083aa9cc0SKonstantin Belousov case PT_NOTE:
161e85eaa93SAndrew Turner if (phdr->p_offset > page_size ||
162e85eaa93SAndrew Turner phdr->p_offset + phdr->p_filesz > page_size) {
163e85eaa93SAndrew Turner note_map_len = rtld_round_page(phdr->p_offset +
164*986c96b5SKonstantin Belousov phdr->p_filesz) -
165*986c96b5SKonstantin Belousov rtld_trunc_page(phdr->p_offset);
166bd56d410SKonstantin Belousov note_map = mmap(NULL, note_map_len, PROT_READ,
167*986c96b5SKonstantin Belousov MAP_PRIVATE, fd,
168*986c96b5SKonstantin Belousov rtld_trunc_page(phdr->p_offset));
169bd56d410SKonstantin Belousov if (note_map == MAP_FAILED) {
170*986c96b5SKonstantin Belousov _rtld_error(
171*986c96b5SKonstantin Belousov "%s: error mapping PT_NOTE (%d)",
172*986c96b5SKonstantin Belousov path, errno);
173bd56d410SKonstantin Belousov goto error;
174bd56d410SKonstantin Belousov }
175*986c96b5SKonstantin Belousov note_start = (Elf_Addr)(note_map +
176*986c96b5SKonstantin Belousov phdr->p_offset -
177e85eaa93SAndrew Turner rtld_trunc_page(phdr->p_offset));
178bd56d410SKonstantin Belousov } else {
179*986c96b5SKonstantin Belousov note_start = (Elf_Addr)(char *)hdr +
180*986c96b5SKonstantin Belousov phdr->p_offset;
181bd56d410SKonstantin Belousov }
18283aa9cc0SKonstantin Belousov note_end = note_start + phdr->p_filesz;
18383aa9cc0SKonstantin Belousov break;
1843124c3e0SJohn Polstra }
1853124c3e0SJohn Polstra
1863124c3e0SJohn Polstra ++phdr;
1873124c3e0SJohn Polstra }
1883124c3e0SJohn Polstra if (phdyn == NULL) {
189bfb1ef60SJohn Polstra _rtld_error("%s: object is not dynamically-linked", path);
190e474e51eSKonstantin Belousov goto error;
1913124c3e0SJohn Polstra }
1923124c3e0SJohn Polstra
1938b7f25d4SAlexander Kabaev if (nsegs < 0) {
194bfb1ef60SJohn Polstra _rtld_error("%s: too few PT_LOAD segments", path);
195e474e51eSKonstantin Belousov goto error;
196bfb1ef60SJohn Polstra }
1973124c3e0SJohn Polstra
1983124c3e0SJohn Polstra /*
1993124c3e0SJohn Polstra * Map the entire address space of the object, to stake out our
2003124c3e0SJohn Polstra * contiguous region, and to establish the base address for relocation.
2013124c3e0SJohn Polstra */
202e85eaa93SAndrew Turner base_vaddr = rtld_trunc_page(segs[0]->p_vaddr);
203*986c96b5SKonstantin Belousov base_vlimit = rtld_round_page(segs[nsegs]->p_vaddr +
204*986c96b5SKonstantin Belousov segs[nsegs]->p_memsz);
2053124c3e0SJohn Polstra mapsize = base_vlimit - base_vaddr;
20615789513STijl Coosemans base_addr = (caddr_t)base_vaddr;
207*986c96b5SKonstantin Belousov base_flags = __getosreldate() >= P_OSREL_MAP_GUARD ?
208*986c96b5SKonstantin Belousov MAP_GUARD : MAP_PRIVATE | MAP_ANON | MAP_NOCORE;
209*986c96b5SKonstantin Belousov if (npagesizes > 1 && rtld_round_page(segs[0]->p_filesz) >=
210*986c96b5SKonstantin Belousov pagesizes[1])
211ea8577c7SAlan Cox base_flags |= MAP_ALIGNED_SUPER;
21291041919SKonstantin Belousov if (base_vaddr != 0)
21391041919SKonstantin Belousov base_flags |= MAP_FIXED | MAP_EXCL;
2143124c3e0SJohn Polstra
215ea8577c7SAlan Cox mapbase = mmap(base_addr, mapsize, PROT_NONE, base_flags, -1, 0);
216f5392eb6SKonstantin Belousov if (mapbase == MAP_FAILED) {
217bfb1ef60SJohn Polstra _rtld_error("%s: mmap of entire address space failed: %s",
2186fea10fbSKonstantin Belousov path, rtld_strerror(errno));
219e474e51eSKonstantin Belousov goto error;
2203124c3e0SJohn Polstra }
2213124c3e0SJohn Polstra if (base_addr != NULL && mapbase != base_addr) {
222*986c96b5SKonstantin Belousov _rtld_error(
223*986c96b5SKonstantin Belousov "%s: mmap returned wrong address: wanted %p, got %p",
224bfb1ef60SJohn Polstra path, base_addr, mapbase);
225e474e51eSKonstantin Belousov goto error1;
2263124c3e0SJohn Polstra }
2273124c3e0SJohn Polstra
2288b7f25d4SAlexander Kabaev for (i = 0; i <= nsegs; i++) {
2298b7f25d4SAlexander Kabaev /* Overlay the segment onto the proper region. */
230e85eaa93SAndrew Turner data_offset = rtld_trunc_page(segs[i]->p_offset);
231e85eaa93SAndrew Turner data_vaddr = rtld_trunc_page(segs[i]->p_vaddr);
232*986c96b5SKonstantin Belousov data_vlimit = rtld_round_page(segs[i]->p_vaddr +
233*986c96b5SKonstantin Belousov segs[i]->p_filesz);
2343124c3e0SJohn Polstra data_addr = mapbase + (data_vaddr - base_vaddr);
235fa7dd9c5SMatthew Dillon data_prot = convert_prot(segs[i]->p_flags);
236fa7dd9c5SMatthew Dillon data_flags = convert_flags(segs[i]->p_flags) | MAP_FIXED;
237*986c96b5SKonstantin Belousov if (data_vlimit != data_vaddr && mmap(data_addr,
238*986c96b5SKonstantin Belousov data_vlimit - data_vaddr, data_prot, data_flags |
239*986c96b5SKonstantin Belousov MAP_PREFAULT_READ, fd, data_offset) == MAP_FAILED) {
240*986c96b5SKonstantin Belousov _rtld_error("%s: mmap of data failed: %s",
241*986c96b5SKonstantin Belousov path, rtld_strerror(errno));
242e474e51eSKonstantin Belousov goto error1;
2433124c3e0SJohn Polstra }
2443124c3e0SJohn Polstra
24569ca61baSKonstantin Belousov /* Do BSS setup */
24669ca61baSKonstantin Belousov if (segs[i]->p_filesz != segs[i]->p_memsz) {
2478b7f25d4SAlexander Kabaev /* Clear any BSS in the last page of the segment. */
2488b7f25d4SAlexander Kabaev clear_vaddr = segs[i]->p_vaddr + segs[i]->p_filesz;
2493124c3e0SJohn Polstra clear_addr = mapbase + (clear_vaddr - base_vaddr);
250*986c96b5SKonstantin Belousov clear_page = mapbase + (rtld_trunc_page(clear_vaddr) -
251*986c96b5SKonstantin Belousov base_vaddr);
25269ca61baSKonstantin Belousov
2538b7f25d4SAlexander Kabaev if ((nclear = data_vlimit - clear_vaddr) > 0) {
254*986c96b5SKonstantin Belousov /*
255*986c96b5SKonstantin Belousov * Make sure the end of the segment is
256*986c96b5SKonstantin Belousov * writable.
257*986c96b5SKonstantin Belousov */
258*986c96b5SKonstantin Belousov if ((data_prot & PROT_WRITE) == 0 &&
259*986c96b5SKonstantin Belousov mprotect(clear_page, page_size,
260*986c96b5SKonstantin Belousov data_prot | PROT_WRITE) == -1) {
261*986c96b5SKonstantin Belousov _rtld_error("%s: mprotect failed: %s",
262*986c96b5SKonstantin Belousov path, rtld_strerror(errno));
263e474e51eSKonstantin Belousov goto error1;
2648b7f25d4SAlexander Kabaev }
2658b7f25d4SAlexander Kabaev
2663124c3e0SJohn Polstra memset(clear_addr, 0, nclear);
2673124c3e0SJohn Polstra
2688b7f25d4SAlexander Kabaev /* Reset the data protection back */
2698b7f25d4SAlexander Kabaev if ((data_prot & PROT_WRITE) == 0)
270*986c96b5SKonstantin Belousov mprotect(clear_page, page_size,
271*986c96b5SKonstantin Belousov data_prot);
2728b7f25d4SAlexander Kabaev }
2738b7f25d4SAlexander Kabaev
2743124c3e0SJohn Polstra /* Overlay the BSS segment onto the proper region. */
2753124c3e0SJohn Polstra bss_vaddr = data_vlimit;
276*986c96b5SKonstantin Belousov bss_vlimit = rtld_round_page(segs[i]->p_vaddr +
277*986c96b5SKonstantin Belousov segs[i]->p_memsz);
2783124c3e0SJohn Polstra bss_addr = mapbase + (bss_vaddr - base_vaddr);
279*986c96b5SKonstantin Belousov if (bss_vlimit > bss_vaddr) {
280*986c96b5SKonstantin Belousov /* There is something to do */
281*986c96b5SKonstantin Belousov if (mmap(bss_addr, bss_vlimit - bss_vaddr,
282*986c96b5SKonstantin Belousov data_prot, data_flags | MAP_ANON, -1,
283*986c96b5SKonstantin Belousov 0) == MAP_FAILED) {
284*986c96b5SKonstantin Belousov _rtld_error(
285*986c96b5SKonstantin Belousov "%s: mmap of bss failed: %s",
286*986c96b5SKonstantin Belousov path, rtld_strerror(errno));
287e474e51eSKonstantin Belousov goto error1;
2883124c3e0SJohn Polstra }
2893124c3e0SJohn Polstra }
29069ca61baSKonstantin Belousov }
29169ca61baSKonstantin Belousov
29249f90ad2SAlexander Kabaev if (phdr_vaddr == 0 && data_offset <= hdr->e_phoff &&
293*986c96b5SKonstantin Belousov data_vlimit - data_vaddr + data_offset >=
294*986c96b5SKonstantin Belousov hdr->e_phoff + hdr->e_phnum * sizeof(Elf_Phdr)) {
29549f90ad2SAlexander Kabaev phdr_vaddr = data_vaddr + hdr->e_phoff - data_offset;
29649f90ad2SAlexander Kabaev }
2978b7f25d4SAlexander Kabaev }
2983124c3e0SJohn Polstra
299926ea445SJohn Polstra obj = obj_new();
3007360ae0fSJohn Polstra if (sb != NULL) {
3017360ae0fSJohn Polstra obj->dev = sb->st_dev;
3027360ae0fSJohn Polstra obj->ino = sb->st_ino;
3037360ae0fSJohn Polstra }
3043124c3e0SJohn Polstra obj->mapbase = mapbase;
3053124c3e0SJohn Polstra obj->mapsize = mapsize;
3063124c3e0SJohn Polstra obj->vaddrbase = base_vaddr;
3073124c3e0SJohn Polstra obj->relocbase = mapbase - base_vaddr;
308a607e5d7SJohn Polstra obj->dynamic = (const Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr);
309341b3de6SMatthew N. Dodd if (hdr->e_entry != 0)
310341b3de6SMatthew N. Dodd obj->entry = (caddr_t)(obj->relocbase + hdr->e_entry);
31149f90ad2SAlexander Kabaev if (phdr_vaddr != 0) {
31249f90ad2SAlexander Kabaev obj->phdr = (const Elf_Phdr *)(obj->relocbase + phdr_vaddr);
31349f90ad2SAlexander Kabaev } else {
31449f90ad2SAlexander Kabaev obj->phdr = malloc(phsize);
31549f90ad2SAlexander Kabaev if (obj->phdr == NULL) {
31649f90ad2SAlexander Kabaev obj_free(obj);
317*986c96b5SKonstantin Belousov _rtld_error("%s: cannot allocate program header",
318*986c96b5SKonstantin Belousov path);
319e474e51eSKonstantin Belousov goto error1;
3203124c3e0SJohn Polstra }
321*986c96b5SKonstantin Belousov memcpy(__DECONST(char *, obj->phdr), (char *)hdr + hdr->e_phoff,
322*986c96b5SKonstantin Belousov phsize);
32349f90ad2SAlexander Kabaev obj->phdr_alloc = true;
32449f90ad2SAlexander Kabaev }
32549f90ad2SAlexander Kabaev obj->phsize = phsize;
326a607e5d7SJohn Polstra if (phinterp != NULL)
327*986c96b5SKonstantin Belousov obj->interp = (const char *)(obj->relocbase +
328*986c96b5SKonstantin Belousov phinterp->p_vaddr);
329017246d0SDoug Rabson if (phtls != NULL) {
330017246d0SDoug Rabson tls_dtv_generation++;
331017246d0SDoug Rabson obj->tlsindex = ++tls_max_index;
332017246d0SDoug Rabson obj->tlssize = phtls->p_memsz;
333017246d0SDoug Rabson obj->tlsalign = phtls->p_align;
3342f06c66aSKonstantin Belousov obj->tlspoffset = phtls->p_offset;
335017246d0SDoug Rabson obj->tlsinitsize = phtls->p_filesz;
336017246d0SDoug Rabson obj->tlsinit = mapbase + phtls->p_vaddr;
337017246d0SDoug Rabson }
338212f264cSKonstantin Belousov obj->stack_flags = stack_flags;
339d958a71bSAlexander Kabaev if (note_start < note_end)
340d958a71bSAlexander Kabaev digest_notes(obj, note_start, note_end);
341bd56d410SKonstantin Belousov if (note_map != NULL)
342bd56d410SKonstantin Belousov munmap(note_map, note_map_len);
343e85eaa93SAndrew Turner munmap(hdr, page_size);
344e474e51eSKonstantin Belousov return (obj);
345e474e51eSKonstantin Belousov
346e474e51eSKonstantin Belousov error1:
347e474e51eSKonstantin Belousov munmap(mapbase, mapsize);
348e474e51eSKonstantin Belousov error:
349bd56d410SKonstantin Belousov if (note_map != NULL && note_map != MAP_FAILED)
350bd56d410SKonstantin Belousov munmap(note_map, note_map_len);
3515d6d106cSKonstantin Belousov if (!phdr_in_zero_page(hdr))
3525d6d106cSKonstantin Belousov munmap(phdr, hdr->e_phnum * sizeof(phdr[0]));
353e85eaa93SAndrew Turner munmap(hdr, page_size);
354e474e51eSKonstantin Belousov return (NULL);
3553124c3e0SJohn Polstra }
3563124c3e0SJohn Polstra
35763fc4e82SKonstantin Belousov bool
check_elf_headers(const Elf_Ehdr * hdr,const char * path)35863fc4e82SKonstantin Belousov check_elf_headers(const Elf_Ehdr *hdr, const char *path)
35963fc4e82SKonstantin Belousov {
36063fc4e82SKonstantin Belousov if (!IS_ELF(*hdr)) {
36163fc4e82SKonstantin Belousov _rtld_error("%s: invalid file format", path);
36263fc4e82SKonstantin Belousov return (false);
36363fc4e82SKonstantin Belousov }
36463fc4e82SKonstantin Belousov if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
36563fc4e82SKonstantin Belousov hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
36663fc4e82SKonstantin Belousov _rtld_error("%s: unsupported file layout", path);
36763fc4e82SKonstantin Belousov return (false);
36863fc4e82SKonstantin Belousov }
36963fc4e82SKonstantin Belousov if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
37063fc4e82SKonstantin Belousov hdr->e_version != EV_CURRENT) {
37163fc4e82SKonstantin Belousov _rtld_error("%s: unsupported file version", path);
37263fc4e82SKonstantin Belousov return (false);
37363fc4e82SKonstantin Belousov }
37463fc4e82SKonstantin Belousov if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
37563fc4e82SKonstantin Belousov _rtld_error("%s: unsupported file type", path);
37663fc4e82SKonstantin Belousov return (false);
37763fc4e82SKonstantin Belousov }
37863fc4e82SKonstantin Belousov if (hdr->e_machine != ELF_TARG_MACH) {
37963fc4e82SKonstantin Belousov _rtld_error("%s: unsupported machine", path);
38063fc4e82SKonstantin Belousov return (false);
38163fc4e82SKonstantin Belousov }
38263fc4e82SKonstantin Belousov if (hdr->e_phentsize != sizeof(Elf_Phdr)) {
38363fc4e82SKonstantin Belousov _rtld_error(
384*986c96b5SKonstantin Belousov "%s: invalid shared object: e_phentsize != sizeof(Elf_Phdr)",
385*986c96b5SKonstantin Belousov path);
38663fc4e82SKonstantin Belousov return (false);
38763fc4e82SKonstantin Belousov }
38863fc4e82SKonstantin Belousov return (true);
38963fc4e82SKonstantin Belousov }
39063fc4e82SKonstantin Belousov
391341b3de6SMatthew N. Dodd static Elf_Ehdr *
get_elf_header(int fd,const char * path,const struct stat * sbp,Elf_Phdr ** phdr_p)3925d6d106cSKonstantin Belousov get_elf_header(int fd, const char *path, const struct stat *sbp,
3935d6d106cSKonstantin Belousov Elf_Phdr **phdr_p)
394341b3de6SMatthew N. Dodd {
395e474e51eSKonstantin Belousov Elf_Ehdr *hdr;
3965d6d106cSKonstantin Belousov Elf_Phdr *phdr;
397341b3de6SMatthew N. Dodd
3987fd852f8SMaxim Sobolev /* Make sure file has enough data for the ELF header */
39978b64846SAlex Richardson if (sbp != NULL && sbp->st_size < (off_t)sizeof(Elf_Ehdr)) {
4007fd852f8SMaxim Sobolev _rtld_error("%s: invalid file format", path);
4017fd852f8SMaxim Sobolev return (NULL);
4027fd852f8SMaxim Sobolev }
4037fd852f8SMaxim Sobolev
404e85eaa93SAndrew Turner hdr = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE | MAP_PREFAULT_READ,
405e474e51eSKonstantin Belousov fd, 0);
406f5392eb6SKonstantin Belousov if (hdr == MAP_FAILED) {
4076fea10fbSKonstantin Belousov _rtld_error("%s: read error: %s", path, rtld_strerror(errno));
408e474e51eSKonstantin Belousov return (NULL);
409341b3de6SMatthew N. Dodd }
410341b3de6SMatthew N. Dodd
411341b3de6SMatthew N. Dodd /* Make sure the file is valid */
41263fc4e82SKonstantin Belousov if (!check_elf_headers(hdr, path))
413e474e51eSKonstantin Belousov goto error;
414341b3de6SMatthew N. Dodd
415341b3de6SMatthew N. Dodd /*
416341b3de6SMatthew N. Dodd * We rely on the program header being in the first page. This is
417341b3de6SMatthew N. Dodd * not strictly required by the ABI specification, but it seems to
418341b3de6SMatthew N. Dodd * always true in practice. And, it simplifies things considerably.
419341b3de6SMatthew N. Dodd */
4205d6d106cSKonstantin Belousov if (phdr_in_zero_page(hdr)) {
4215d6d106cSKonstantin Belousov phdr = (Elf_Phdr *)((char *)hdr + hdr->e_phoff);
4225d6d106cSKonstantin Belousov } else {
423*986c96b5SKonstantin Belousov phdr = mmap(NULL, hdr->e_phnum * sizeof(phdr[0]), PROT_READ,
424*986c96b5SKonstantin Belousov MAP_PRIVATE | MAP_PREFAULT_READ, fd, hdr->e_phoff);
4255d6d106cSKonstantin Belousov if (phdr == MAP_FAILED) {
4265d6d106cSKonstantin Belousov _rtld_error("%s: error mapping phdr: %s", path,
4275d6d106cSKonstantin Belousov rtld_strerror(errno));
428e474e51eSKonstantin Belousov goto error;
429341b3de6SMatthew N. Dodd }
4305d6d106cSKonstantin Belousov }
4315d6d106cSKonstantin Belousov *phdr_p = phdr;
432e474e51eSKonstantin Belousov return (hdr);
433341b3de6SMatthew N. Dodd
434e474e51eSKonstantin Belousov error:
435e85eaa93SAndrew Turner munmap(hdr, page_size);
436e474e51eSKonstantin Belousov return (NULL);
437341b3de6SMatthew N. Dodd }
438341b3de6SMatthew N. Dodd
439926ea445SJohn Polstra void
obj_free(Obj_Entry * obj)440926ea445SJohn Polstra obj_free(Obj_Entry *obj)
441926ea445SJohn Polstra {
442926ea445SJohn Polstra Objlist_Entry *elm;
443926ea445SJohn Polstra
444283a4f40SKonstantin Belousov if (obj->tls_static)
445ddab7ee8SDoug Rabson free_tls_offset(obj);
446926ea445SJohn Polstra while (obj->needed != NULL) {
447926ea445SJohn Polstra Needed_Entry *needed = obj->needed;
448*986c96b5SKonstantin Belousov
449926ea445SJohn Polstra obj->needed = needed->next;
450926ea445SJohn Polstra free(needed);
451926ea445SJohn Polstra }
4520eb88f20SAlexander Kabaev while (!STAILQ_EMPTY(&obj->names)) {
4530eb88f20SAlexander Kabaev Name_Entry *entry = STAILQ_FIRST(&obj->names);
454*986c96b5SKonstantin Belousov
4550eb88f20SAlexander Kabaev STAILQ_REMOVE_HEAD(&obj->names, link);
4560eb88f20SAlexander Kabaev free(entry);
4570eb88f20SAlexander Kabaev }
458926ea445SJohn Polstra while (!STAILQ_EMPTY(&obj->dldags)) {
459926ea445SJohn Polstra elm = STAILQ_FIRST(&obj->dldags);
460926ea445SJohn Polstra STAILQ_REMOVE_HEAD(&obj->dldags, link);
461926ea445SJohn Polstra free(elm);
462926ea445SJohn Polstra }
463926ea445SJohn Polstra while (!STAILQ_EMPTY(&obj->dagmembers)) {
464926ea445SJohn Polstra elm = STAILQ_FIRST(&obj->dagmembers);
465926ea445SJohn Polstra STAILQ_REMOVE_HEAD(&obj->dagmembers, link);
466926ea445SJohn Polstra free(elm);
467926ea445SJohn Polstra }
46849f90ad2SAlexander Kabaev if (obj->vertab)
4690eb88f20SAlexander Kabaev free(obj->vertab);
47049f90ad2SAlexander Kabaev if (obj->origin_path)
471da9f2454SMatthew N. Dodd free(obj->origin_path);
47228551690SKonstantin Belousov if (obj->z_origin)
473903e0ffdSAlex Richardson free(__DECONST(void *, obj->rpath));
47449f90ad2SAlexander Kabaev if (obj->priv)
47563c1e7cbSAlexander Kabaev free(obj->priv);
47649f90ad2SAlexander Kabaev if (obj->path)
47749f90ad2SAlexander Kabaev free(obj->path);
47849f90ad2SAlexander Kabaev if (obj->phdr_alloc)
479903e0ffdSAlex Richardson free(__DECONST(void *, obj->phdr));
480926ea445SJohn Polstra free(obj);
481926ea445SJohn Polstra }
482926ea445SJohn Polstra
483926ea445SJohn Polstra Obj_Entry *
obj_new(void)484926ea445SJohn Polstra obj_new(void)
485926ea445SJohn Polstra {
486926ea445SJohn Polstra Obj_Entry *obj;
487926ea445SJohn Polstra
488926ea445SJohn Polstra obj = CNEW(Obj_Entry);
489926ea445SJohn Polstra STAILQ_INIT(&obj->dldags);
490926ea445SJohn Polstra STAILQ_INIT(&obj->dagmembers);
4910eb88f20SAlexander Kabaev STAILQ_INIT(&obj->names);
492*986c96b5SKonstantin Belousov return (obj);
493926ea445SJohn Polstra }
494926ea445SJohn Polstra
4953124c3e0SJohn Polstra /*
4963124c3e0SJohn Polstra * Given a set of ELF protection flags, return the corresponding protection
4973124c3e0SJohn Polstra * flags for MMAP.
4983124c3e0SJohn Polstra */
499ca8c8dc3SKonstantin Belousov int
convert_prot(int elfflags)500fa7dd9c5SMatthew Dillon convert_prot(int elfflags)
5013124c3e0SJohn Polstra {
5023124c3e0SJohn Polstra int prot = 0;
503*986c96b5SKonstantin Belousov
504*986c96b5SKonstantin Belousov if ((elfflags & PF_R) != 0)
5053124c3e0SJohn Polstra prot |= PROT_READ;
506*986c96b5SKonstantin Belousov if ((elfflags & PF_W) != 0)
5073124c3e0SJohn Polstra prot |= PROT_WRITE;
508*986c96b5SKonstantin Belousov if ((elfflags & PF_X) != 0)
5093124c3e0SJohn Polstra prot |= PROT_EXEC;
510*986c96b5SKonstantin Belousov return (prot);
5113124c3e0SJohn Polstra }
512fa7dd9c5SMatthew Dillon
513fa7dd9c5SMatthew Dillon static int
convert_flags(int elfflags)514fa7dd9c5SMatthew Dillon convert_flags(int elfflags)
515fa7dd9c5SMatthew Dillon {
516fa7dd9c5SMatthew Dillon int flags = MAP_PRIVATE; /* All mappings are private */
517fa7dd9c5SMatthew Dillon
518fa7dd9c5SMatthew Dillon /*
519fa7dd9c5SMatthew Dillon * Readonly mappings are marked "MAP_NOCORE", because they can be
520fa7dd9c5SMatthew Dillon * reconstructed by a debugger.
521fa7dd9c5SMatthew Dillon */
522*986c96b5SKonstantin Belousov if ((elfflags & PF_W) == 0)
523fa7dd9c5SMatthew Dillon flags |= MAP_NOCORE;
524*986c96b5SKonstantin Belousov return (flags);
525fa7dd9c5SMatthew Dillon }
526