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