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