xref: /freebsd/lib/libc/gen/elf_utils.c (revision 250b2eda0acc44cf882d5ea8fcf28125e7501719)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Konstantin Belousov <kib@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Neither the name of the author nor the names of any co-contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/auxv.h>
31 #include <sys/mman.h>
32 #include <sys/resource.h>
33 #include <sys/sysctl.h>
34 
35 #include <machine/tls.h>
36 
37 #include <link.h>
38 #include <stddef.h>
39 #include <string.h>
40 
41 #include "libc_private.h"
42 
43 void __pthread_map_stacks_exec(void);
44 void __pthread_distribute_static_tls(size_t, void *, size_t, size_t);
45 
46 int
__elf_phdr_match_addr(struct dl_phdr_info * phdr_info,void * addr)47 __elf_phdr_match_addr(struct dl_phdr_info *phdr_info, void *addr)
48 {
49 	const Elf_Phdr *ph;
50 	int i;
51 
52 	for (i = 0; i < phdr_info->dlpi_phnum; i++) {
53 		ph = &phdr_info->dlpi_phdr[i];
54 		if (ph->p_type != PT_LOAD)
55 			continue;
56 
57 		/* ELFv1 ABI for powerpc64 passes function descriptor
58 		 * pointers around, not function pointers.  The function
59 		 * descriptors live in .opd, which is a non-executable segment.
60 		 * The PF_X check would therefore make all address checks fail,
61 		 * causing a crash in some instances.  Don't skip over
62 		 * non-executable segments in the ELFv1 powerpc64 case.
63 		 */
64 #if !defined(__powerpc64__) || (defined(_CALL_ELF) && _CALL_ELF == 2)
65 		if ((ph->p_flags & PF_X) == 0)
66 			continue;
67 #endif
68 
69 		if (phdr_info->dlpi_addr + ph->p_vaddr <= (uintptr_t)addr &&
70 		    (uintptr_t)addr < phdr_info->dlpi_addr +
71 		    ph->p_vaddr + ph->p_memsz)
72 			break;
73 	}
74 	return (i != phdr_info->dlpi_phnum);
75 }
76 
77 void
__libc_map_stacks_exec(void)78 __libc_map_stacks_exec(void)
79 {
80 	int mib[2];
81 	struct rlimit rlim;
82 	u_long usrstack, stacksz;
83 	size_t len;
84 
85 	if (_elf_aux_info(AT_USRSTACKBASE, &usrstack, sizeof(usrstack)) != 0) {
86 		mib[0] = CTL_KERN;
87 		mib[1] = KERN_USRSTACK;
88 		len = sizeof(usrstack);
89 		if (sysctl(mib, nitems(mib), &usrstack, &len, NULL, 0) == -1)
90 			return;
91 	}
92 	if (_elf_aux_info(AT_USRSTACKLIM, &stacksz, sizeof(stacksz)) != 0) {
93 		if (getrlimit(RLIMIT_STACK, &rlim) == -1)
94 			return;
95 		stacksz = rlim.rlim_cur;
96 	}
97 	mprotect((void *)(uintptr_t)(usrstack - stacksz), stacksz,
98 	    _rtld_get_stack_prot());
99 }
100 
101 #pragma weak __pthread_map_stacks_exec
102 void
__pthread_map_stacks_exec(void)103 __pthread_map_stacks_exec(void)
104 {
105 
106 	((void (*)(void))__libc_interposing[INTERPOS_map_stacks_exec])();
107 }
108 
109 void
__libc_distribute_static_tls(size_t offset,void * src,size_t len,size_t total_len)110 __libc_distribute_static_tls(size_t offset, void *src, size_t len,
111     size_t total_len)
112 {
113 	char *tlsbase;
114 
115 #ifdef TLS_VARIANT_I
116 	tlsbase = (char *)_tcb_get() + offset;
117 #else
118 	tlsbase = (char *)_tcb_get() - offset;
119 #endif
120 	memcpy(tlsbase, src, len);
121 	memset(tlsbase + len, 0, total_len - len);
122 }
123 
124 #pragma weak __pthread_distribute_static_tls
125 void
__pthread_distribute_static_tls(size_t offset,void * src,size_t len,size_t total_len)126 __pthread_distribute_static_tls(size_t offset, void *src, size_t len,
127     size_t total_len)
128 {
129 
130 	((void (*)(size_t, void *, size_t, size_t))__libc_interposing[
131 	    INTERPOS_distribute_static_tls])(offset, src, len, total_len);
132 }
133