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