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