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