xref: /titanic_50/usr/src/uts/sparc/krtld/kobj_reloc.c (revision 986fd29a0dc13f7608ef7f508f6e700bd7bc2720)
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 #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 "krtld/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
tnf_reloc_resolve(char * symname,Addr * value_p,Elf64_Sxword * addend_p,long offset,tnf_probe_control_t ** probelist,tnf_tag_data_t ** taglist)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
sdt_reloc_resolve(struct module * mp,char * symname,uint32_t * instr,long roff)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 */
do_relocate(struct module * mp,char * reltbl,Word relshtype,int nreloc,int relocsize,Addr baseaddr)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_INS(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(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, off, &probelist, &taglist) != 0) {
281 					if (ELF_ST_BIND(symref->st_info)
282 					    != STB_WEAK) {
283 						_kobj_printf(ops,
284 						    "not found: %s\n",
285 						    mp->strings +
286 						    symref->st_name);
287 						err = 1;
288 					}
289 					continue;
290 				} else { /* symbol found  - relocate */
291 					/*
292 					 * calculate location of definition
293 					 * - symbol value plus base address of
294 					 * containing shared object
295 					 */
296 					value = symref->st_value;
297 				} /* end else symbol found */
298 			}
299 		} /* end not R_SPARC_RELATIVE */
300 
301 		value += addend;
302 		if (IS_EXTOFFSET(rtype)) {
303 			value +=
304 			    (Word) ELF_R_TYPE_DATA(((Rela *)reladdr)->r_info);
305 		}
306 
307 		/*
308 		 * calculate final value -
309 		 * if PC-relative, subtract ref addr
310 		 */
311 		if (IS_PC_RELATIVE(rtype)) {
312 			if (mp->destination)
313 				value -= (baseaddr + roff);
314 			else
315 				value -= off;
316 		}
317 
318 #ifdef	KOBJ_DEBUG
319 		if (kobj_debug & D_RELOCATIONS) {
320 			_kobj_printf(ops, "krtld:\t\t\t\t0x%8llx", off);
321 			_kobj_printf(ops, " 0x%8llx\n", value);
322 		}
323 #endif
324 		if (do_reloc_krtld(rtype, (unsigned char *)off, (Xword *)&value,
325 		    (const char *)mp->strings + symref->st_name,
326 		    mp->filename) == 0)
327 			err = 1;
328 	} /* end of while loop */
329 
330 	if (err)
331 		return (-1);
332 
333 	if (tnf_splice_probes(mp->flags & KOBJ_PRIM, probelist, taglist))
334 		mp->flags |= KOBJ_TNF_PROBE;
335 
336 	return (0);
337 }
338 
339 int
do_relocations(struct module * mp)340 do_relocations(struct module *mp)
341 {
342 	uint_t shn;
343 	Shdr *shp, *rshp;
344 	uint_t nreloc;
345 
346 	/* do the relocations */
347 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
348 		rshp = (Shdr *)
349 		    (mp->shdrs + shn * mp->hdr.e_shentsize);
350 		if (rshp->sh_type == SHT_REL) {
351 			_kobj_printf(ops, "%s can't process type SHT_REL\n",
352 			    mp->filename);
353 			return (-1);
354 		}
355 		if (rshp->sh_type != SHT_RELA)
356 			continue;
357 		if (rshp->sh_link != mp->symtbl_section) {
358 			_kobj_printf(ops, "%s reloc for non-default symtab\n",
359 			    mp->filename);
360 			return (-1);
361 		}
362 		if (rshp->sh_info >= mp->hdr.e_shnum) {
363 			_kobj_printf(ops, "do_relocations: %s ", mp->filename);
364 			_kobj_printf(ops, " sh_info out of range %lld\n", shn);
365 			goto bad;
366 		}
367 		nreloc = rshp->sh_size / rshp->sh_entsize;
368 
369 		/* get the section header that this reloc table refers to */
370 		shp = (Shdr *)
371 		    (mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize);
372 		/*
373 		 * Do not relocate any section that isn't loaded into memory.
374 		 * Most commonly this will skip over the .rela.stab* sections
375 		 */
376 		if (!(shp->sh_flags & SHF_ALLOC))
377 			continue;
378 #ifdef	KOBJ_DEBUG
379 		if (kobj_debug & D_RELOCATIONS) {
380 			_kobj_printf(ops, "krtld: relocating: file=%s ",
381 			    mp->filename);
382 			_kobj_printf(ops, " section=%d\n", shn);
383 		}
384 #endif
385 		if (do_relocate(mp, (char *)rshp->sh_addr, rshp->sh_type,
386 		    nreloc, rshp->sh_entsize, shp->sh_addr) < 0) {
387 			_kobj_printf(ops,
388 			    "do_relocations: %s do_relocate failed\n",
389 			    mp->filename);
390 			goto bad;
391 		}
392 		kobj_free((void *)rshp->sh_addr, rshp->sh_size);
393 		rshp->sh_addr = 0;
394 	}
395 	mp->flags |= KOBJ_RELOCATED;
396 	return (0);
397 bad:
398 	kobj_free((void *)rshp->sh_addr, rshp->sh_size);
399 	rshp->sh_addr = 0;
400 	return (-1);
401 }
402