1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright 2020 Joyent, Inc.
27  * Copyright 2024 Oxide Computer Company
28  */
29 
30 /*
31  * x86 relocation code.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/sysmacros.h>
37 #include <sys/systm.h>
38 #include <sys/user.h>
39 #include <sys/bootconf.h>
40 #include <sys/modctl.h>
41 #include <sys/elf.h>
42 #include <sys/kobj.h>
43 #include <sys/kobj_impl.h>
44 #include <sys/sdt_impl.h>
45 
46 #include "reloc.h"
47 
48 static int
49 sdt_reloc_resolve(struct module *mp, char *symname, uint8_t *instr)
50 {
51 	sdt_probedesc_t *sdp;
52 
53 	/*
54 	 * The "statically defined tracing" (SDT) provider for DTrace.
55 	 * The SDT mechanism works by replacing calls to the
56 	 * undefined routine __dtrace_probe_[name] with nop instructions.
57 	 * The relocations are logged, and SDT itself will later patch the
58 	 * running binary appropriately.
59 	 */
60 	if (strncmp(symname, sdt_prefix, strlen(sdt_prefix)) != 0)
61 		return (1);
62 
63 	symname += strlen(sdt_prefix);
64 
65 	sdp = kobj_alloc(sizeof (sdt_probedesc_t), KM_WAIT);
66 	sdp->sdpd_name = kobj_alloc(strlen(symname) + 1, KM_WAIT);
67 	bcopy(symname, sdp->sdpd_name, strlen(symname) + 1);
68 
69 	sdp->sdpd_offset = (uintptr_t)instr;
70 	sdp->sdpd_next = mp->sdt_probes;
71 	mp->sdt_probes = sdp;
72 
73 	/*
74 	 * The compiler may emit the probe call as a tail call (/sibling call).
75 	 * On x86 that means instead of a CALL instruction, we get a JMP.  In
76 	 * that case, we also need to patch in a RET instruction instead of just
77 	 * NOPs.
78 	 */
79 	const boolean_t is_tailcall = instr[-1] != SDT_CALL;
80 	instr[-1] = SDT_NOP;
81 	instr[0] = SDT_NOP;
82 	instr[1] = SDT_NOP;
83 	instr[2] = SDT_NOP;
84 	instr[SDT_OFF_RET_IDX] = is_tailcall ? SDT_RET : SDT_NOP;
85 
86 	return (0);
87 }
88 
89 
90 /*
91  * We're relying on the fact that the call we're replacing is
92  * call (e8) plus 4 bytes of address, making a 5 byte instruction
93  */
94 #define	NOP_INSTR	0x90
95 #define	SMAP_NOPS	5
96 
97 /*
98  * Currently the only call replaced as a hot inline
99  * is smap_enable() and smap_disable(). If more are needed
100  * we should probably come up with an sdt probe like prefix
101  * and look for those instead of exact call names.
102  */
103 static int
104 smap_reloc_resolve(struct module *mp, char *symname, uint8_t *instr)
105 {
106 	uint_t symlen;
107 	hotinline_desc_t *hid;
108 
109 	if (strcmp(symname, "smap_enable") == 0 ||
110 	    strcmp(symname, "smap_disable") == 0) {
111 
112 #ifdef	KOBJ_DEBUG
113 		if (kobj_debug & D_RELOCATIONS) {
114 			_kobj_printf(ops, "smap_reloc_resolve: %s relocating "
115 			    "enable/disable_smap\n", mp->filename);
116 		}
117 #endif
118 
119 		hid = kobj_alloc(sizeof (hotinline_desc_t), KM_WAIT);
120 		symlen = strlen(symname) + 1;
121 		hid->hid_symname = kobj_alloc(symlen, KM_WAIT);
122 		bcopy(symname, hid->hid_symname, symlen);
123 
124 		/*
125 		 * We backtrack one byte here to consume the call
126 		 * instruction itself.
127 		 */
128 		hid->hid_instr_offset = (uintptr_t)instr - 1;
129 		hid->hid_next = mp->hi_calls;
130 		mp->hi_calls = hid;
131 
132 		memset((void *)hid->hid_instr_offset, NOP_INSTR, SMAP_NOPS);
133 
134 		return (0);
135 	}
136 
137 	return (1);
138 }
139 
140 int
141 do_relocate(struct module *mp, char *reltbl, int nreloc, int relocsize,
142     Addr baseaddr)
143 {
144 	unsigned long stndx;
145 	unsigned long off;
146 	register unsigned long reladdr, rend;
147 	register unsigned int rtype;
148 	unsigned long value;
149 	Elf64_Sxword addend;
150 	Sym *symref = NULL;
151 	int err = 0;
152 	int symnum;
153 	reladdr = (unsigned long)reltbl;
154 	rend = reladdr + nreloc * relocsize;
155 
156 #ifdef	KOBJ_DEBUG
157 	if (kobj_debug & D_RELOCATIONS) {
158 		_kobj_printf(ops, "krtld:\ttype\t\t\toffset\t   addend"
159 		    "      symbol\n");
160 		_kobj_printf(ops, "krtld:\t\t\t\t\t   value\n");
161 	}
162 #endif
163 
164 	symnum = -1;
165 	/* loop through relocations */
166 	while (reladdr < rend) {
167 		symnum++;
168 		rtype = ELF_R_TYPE(((Rela *)reladdr)->r_info);
169 		off = ((Rela *)reladdr)->r_offset;
170 		stndx = ELF_R_SYM(((Rela *)reladdr)->r_info);
171 		if (stndx >= mp->nsyms) {
172 			_kobj_printf(ops, "do_relocate: bad strndx %d\n",
173 			    symnum);
174 			return (-1);
175 		}
176 		if ((rtype > R_AMD64_NUM) || IS_TLS_INS(rtype)) {
177 			_kobj_printf(ops, "krtld: invalid relocation type %d",
178 			    rtype);
179 			_kobj_printf(ops, " at 0x%lx:", off);
180 			_kobj_printf(ops, " file=%s\n", mp->filename);
181 			return (-1);
182 		}
183 
184 
185 		addend = (long)(((Rela *)reladdr)->r_addend);
186 		reladdr += relocsize;
187 
188 
189 		if (rtype == R_AMD64_NONE)
190 			continue;
191 
192 #ifdef	KOBJ_DEBUG
193 		if (kobj_debug & D_RELOCATIONS) {
194 			Sym *	symp;
195 			symp = (Sym *)
196 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
197 			_kobj_printf(ops, "krtld:\t%s",
198 			    conv_reloc_amd64_type(rtype));
199 			_kobj_printf(ops, "\t0x%8lx", off);
200 			_kobj_printf(ops, " %8lld", (longlong_t)addend);
201 			_kobj_printf(ops, "  %s\n",
202 			    (const char *)mp->strings + symp->st_name);
203 		}
204 #endif
205 
206 		if (!(mp->flags & KOBJ_EXEC))
207 			off += baseaddr;
208 
209 		/*
210 		 * if R_AMD64_RELATIVE, simply add base addr
211 		 * to reloc location
212 		 */
213 
214 		if (rtype == R_AMD64_RELATIVE) {
215 			value = baseaddr;
216 		} else {
217 			/*
218 			 * get symbol table entry - if symbol is local
219 			 * value is base address of this object
220 			 */
221 			symref = (Sym *)
222 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
223 
224 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
225 				/* *** this is different for .o and .so */
226 				value = symref->st_value;
227 			} else {
228 				/*
229 				 * It's global. Allow weak references.  If
230 				 * the symbol is undefined, give dtrace
231 				 * a chance to see if it's a probe site,
232 				 * and fix it up if so.
233 				 */
234 				if (symref->st_shndx == SHN_UNDEF &&
235 				    sdt_reloc_resolve(mp, mp->strings +
236 				    symref->st_name, (uint8_t *)off) == 0)
237 					continue;
238 
239 				if (symref->st_shndx == SHN_UNDEF &&
240 				    smap_reloc_resolve(mp, mp->strings +
241 				    symref->st_name, (uint8_t *)off) == 0)
242 					continue;
243 
244 				if (symref->st_shndx == SHN_UNDEF) {
245 					if (ELF_ST_BIND(symref->st_info)
246 					    != STB_WEAK) {
247 						_kobj_printf(ops,
248 						    "not found: %s\n",
249 						    mp->strings +
250 						    symref->st_name);
251 						err = 1;
252 					}
253 					continue;
254 				} else { /* symbol found  - relocate */
255 					/*
256 					 * calculate location of definition
257 					 * - symbol value plus base address of
258 					 * containing shared object
259 					 */
260 					value = symref->st_value;
261 
262 				} /* end else symbol found */
263 			} /* end global or weak */
264 		} /* end not R_AMD64_RELATIVE */
265 
266 		value += addend;
267 		/*
268 		 * calculate final value -
269 		 * if PC-relative, subtract ref addr
270 		 */
271 		if (IS_PC_RELATIVE(rtype))
272 			value -= off;
273 
274 #ifdef	KOBJ_DEBUG
275 		if (kobj_debug & D_RELOCATIONS) {
276 			_kobj_printf(ops, "krtld:\t\t\t\t0x%8lx", off);
277 			_kobj_printf(ops, " 0x%8lx\n", value);
278 		}
279 #endif
280 
281 		if (do_reloc_krtld(rtype, (unsigned char *)off, &value,
282 		    (const char *)mp->strings + symref->st_name,
283 		    mp->filename) == 0)
284 			err = 1;
285 
286 	} /* end of while loop */
287 	if (err)
288 		return (-1);
289 
290 	return (0);
291 }
292 
293 int
294 do_relocations(struct module *mp)
295 {
296 	uint_t shn;
297 	Shdr *shp, *rshp;
298 	uint_t nreloc;
299 
300 	/* do the relocations */
301 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
302 		rshp = (Shdr *)
303 		    (mp->shdrs + shn * mp->hdr.e_shentsize);
304 		if (rshp->sh_type == SHT_REL) {
305 			_kobj_printf(ops, "%s can't process type SHT_REL\n",
306 			    mp->filename);
307 			return (-1);
308 		}
309 		if (rshp->sh_type != SHT_RELA)
310 			continue;
311 		if (rshp->sh_link != mp->symtbl_section) {
312 			_kobj_printf(ops, "%s reloc for non-default symtab\n",
313 			    mp->filename);
314 			return (-1);
315 		}
316 		if (rshp->sh_info >= mp->hdr.e_shnum) {
317 			_kobj_printf(ops, "do_relocations: %s sh_info ",
318 			    mp->filename);
319 			_kobj_printf(ops, "out of range %d\n", shn);
320 			goto bad;
321 		}
322 		nreloc = rshp->sh_size / rshp->sh_entsize;
323 
324 		/* get the section header that this reloc table refers to */
325 		shp = (Shdr *)
326 		    (mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize);
327 
328 		/*
329 		 * Do not relocate any section that isn't loaded into memory.
330 		 * Most commonly this will skip over the .rela.stab* sections
331 		 */
332 		if (!(shp->sh_flags & SHF_ALLOC))
333 			continue;
334 #ifdef	KOBJ_DEBUG
335 		if (kobj_debug & D_RELOCATIONS) {
336 			_kobj_printf(ops, "krtld: relocating: file=%s ",
337 			    mp->filename);
338 			_kobj_printf(ops, "section=%d\n", shn);
339 		}
340 #endif
341 
342 		if (do_relocate(mp, (char *)rshp->sh_addr, nreloc,
343 		    rshp->sh_entsize, shp->sh_addr) < 0) {
344 			_kobj_printf(ops,
345 			    "do_relocations: %s do_relocate failed\n",
346 			    mp->filename);
347 			goto bad;
348 		}
349 		kobj_free((void *)rshp->sh_addr, rshp->sh_size);
350 		rshp->sh_addr = 0;
351 	}
352 	mp->flags |= KOBJ_RELOCATED;
353 	return (0);
354 bad:
355 	kobj_free((void *)rshp->sh_addr, rshp->sh_size);
356 	rshp->sh_addr = 0;
357 	return (-1);
358 }
359