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