xref: /illumos-gate/usr/src/uts/sparc/krtld/kobj_reloc.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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/sdt.h>
42 
43 #include "krtld/reloc.h"
44 
45 #define	SDT_RESTORE_MASK	0xc1f80000
46 #define	SDT_RESTORE		0x81e80000
47 #define	SDT_NOP			0x01000000
48 #define	SDT_RET			0x81c7e008
49 #define	SDT_RETL		0x81c3e008
50 #define	SDT_RDO7_MASK		0xbf000000
51 #define	SDT_RDO7		0x9e000000
52 
53 static int
54 sdt_reloc_resolve(struct module *mp, char *symname, uint32_t *instr, long roff)
55 {
56 	sdt_probedesc_t *sdp;
57 
58 	/*
59 	 * The "statically defined tracing" (SDT) provider for DTrace.
60 	 * The SDT mechanism works by replacing calls to the
61 	 * undefined routine __dtrace_probe_[name] with nop instructions.
62 	 * The relocations are logged, and SDT itself will later patch the
63 	 * running binary appropriately.
64 	 */
65 	if (strncmp(symname, sdt_prefix, strlen(sdt_prefix)) != 0)
66 		return (1);
67 
68 	symname += strlen(sdt_prefix);
69 
70 	sdp = kobj_alloc(sizeof (sdt_probedesc_t), KM_WAIT);
71 	sdp->sdpd_name = kobj_alloc(strlen(symname) + 1, KM_WAIT);
72 	bcopy(symname, sdp->sdpd_name, strlen(symname) + 1);
73 
74 	if ((uint32_t *)roff == instr) {
75 		/*
76 		 * This isn't an offset -- it's an absolute value.  (This is
77 		 * typically only true for "unix".)  We need to convert the
78 		 * value into an offset from mp->text.
79 		 */
80 		roff -= (uintptr_t)mp->text;
81 	}
82 
83 	sdp->sdpd_offset = roff;
84 
85 	sdp->sdpd_next = mp->sdt_probes;
86 	mp->sdt_probes = sdp;
87 
88 	/*
89 	 * If the next instruction is a restore (any variant), then the probe
90 	 * point is being tail-called.  Instead of patching the call to be a
91 	 * NOP, we must patch it to be a ret.  If the next instruction is
92 	 * writing to %o7, it must be a tail call from a leaf; we must patch
93 	 * the instruction to be a retl.
94 	 */
95 	if ((*(instr + 1) & SDT_RESTORE_MASK) == SDT_RESTORE) {
96 		*instr = SDT_RET;
97 	} else if ((*(instr + 1) & SDT_RDO7_MASK) == SDT_RDO7) {
98 		*instr = SDT_RETL;
99 	} else {
100 		*instr = SDT_NOP;
101 	}
102 
103 	return (0);
104 }
105 
106 int
107 do_relocate(struct module *mp, char *reltbl, int nreloc, int relocsize,
108     Addr baseaddr)
109 {
110 	Word stndx;
111 	long off, roff;
112 	uintptr_t reladdr, rend;
113 	uint_t rtype;
114 	Elf64_Sxword addend;
115 	Addr value, destination;
116 	Sym *symref;
117 	int symnum;
118 	int err = 0;
119 
120 	reladdr = (uintptr_t)reltbl;
121 	rend = reladdr + nreloc * relocsize;
122 
123 #ifdef	KOBJ_DEBUG
124 	if (kobj_debug & D_RELOCATIONS) {
125 		_kobj_printf(ops, "krtld:\ttype\t\t\toffset\t   addend"
126 		    "      symbol\n");
127 		_kobj_printf(ops, "krtld:\t\t\t\t\t   value\n");
128 	}
129 #endif
130 	destination = baseaddr;
131 
132 	/*
133 	 * If this machine is loading a module through an alternate address
134 	 * we need to compute the spot where the actual relocation will
135 	 * take place.
136 	 */
137 	if (mp->destination) {
138 		int i;
139 		Shdr * shp;
140 		shp = (Shdr *)mp->shdrs;
141 		for (i = 0; i < mp->hdr.e_shnum; i++, shp++) {
142 			if (shp->sh_addr == baseaddr) {
143 				if ((shp->sh_flags & SHF_ALLOC) &&
144 				    !(shp->sh_flags & SHF_WRITE))
145 					destination = (Addr)mp->destination +
146 					    (baseaddr - (Addr)mp->text);
147 				break;
148 			}
149 		}
150 	}
151 
152 	symnum = -1;
153 	/* loop through relocations */
154 	while (reladdr < rend) {
155 
156 		symnum++;
157 		rtype = ELF_R_TYPE(((Rela *)reladdr)->r_info);
158 		roff = off = ((Rela *)reladdr)->r_offset;
159 		stndx = ELF_R_SYM(((Rela *)reladdr)->r_info);
160 		if (stndx >= mp->nsyms) {
161 			_kobj_printf(ops,
162 			    "do_relocate: bad strndx %d\n", symnum);
163 			return (-1);
164 		}
165 		if ((rtype > R_SPARC_NUM) || IS_TLS_INS(rtype)) {
166 			_kobj_printf(ops, "krtld: invalid relocation type %d",
167 			    rtype);
168 			_kobj_printf(ops, " at 0x%llx:", (u_longlong_t)off);
169 			_kobj_printf(ops, " file=%s\n", mp->filename);
170 			err = 1;
171 			continue;
172 		}
173 		addend = (long)(((Rela *)reladdr)->r_addend);
174 		reladdr += relocsize;
175 
176 
177 #ifdef	KOBJ_DEBUG
178 		if (kobj_debug & D_RELOCATIONS) {
179 			Sym *symp;
180 			symp = (Sym *)
181 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
182 			_kobj_printf(ops, "krtld:\t%s",
183 			    conv_reloc_SPARC_type(rtype));
184 			_kobj_printf(ops, "\t0x%8llx", (u_longlong_t)off);
185 			_kobj_printf(ops, " 0x%8llx", (u_longlong_t)addend);
186 			_kobj_printf(ops, "  %s\n",
187 			    (const char *)mp->strings + symp->st_name);
188 		}
189 #endif
190 
191 		if (rtype == R_SPARC_NONE)
192 			continue;
193 
194 		if (!(mp->flags & KOBJ_EXEC))
195 			off += destination;
196 
197 		/*
198 		 * if R_SPARC_RELATIVE, simply add base addr
199 		 * to reloc location
200 		 */
201 		if (rtype == R_SPARC_RELATIVE) {
202 			value = baseaddr;
203 		} else {
204 			/*
205 			 * get symbol table entry - if symbol is local
206 			 * value is base address of this object
207 			 */
208 			symref = (Sym *)
209 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
210 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
211 				/* *** this is different for .o and .so */
212 				value = symref->st_value;
213 			} else {
214 				/*
215 				 * It's global. Allow weak references.  If
216 				 * the symbol is undefined, give dtrace
217 				 * a chance to see if it's a probe site,
218 				 * and fix it up if so.
219 				 */
220 				if (symref->st_shndx == SHN_UNDEF &&
221 				    sdt_reloc_resolve(mp, mp->strings +
222 				    symref->st_name, (uint32_t *)off,
223 				    roff + ((uintptr_t)baseaddr -
224 				    (uintptr_t)mp->text)) == 0)
225 					continue;
226 
227 				if (symref->st_shndx == SHN_UNDEF) {
228 					if (ELF_ST_BIND(symref->st_info)
229 					    != STB_WEAK) {
230 						_kobj_printf(ops,
231 						    "not found: %s\n",
232 						    mp->strings +
233 						    symref->st_name);
234 						err = 1;
235 					}
236 					continue;
237 				} else { /* symbol found  - relocate */
238 					/*
239 					 * calculate location of definition
240 					 * - symbol value plus base address of
241 					 * containing shared object
242 					 */
243 					value = symref->st_value;
244 				} /* end else symbol found */
245 			}
246 		} /* end not R_SPARC_RELATIVE */
247 
248 		value += addend;
249 		if (IS_EXTOFFSET(rtype)) {
250 			value +=
251 			    (Word) ELF_R_TYPE_DATA(((Rela *)reladdr)->r_info);
252 		}
253 
254 		/*
255 		 * calculate final value -
256 		 * if PC-relative, subtract ref addr
257 		 */
258 		if (IS_PC_RELATIVE(rtype)) {
259 			if (mp->destination)
260 				value -= (baseaddr + roff);
261 			else
262 				value -= off;
263 		}
264 
265 #ifdef	KOBJ_DEBUG
266 		if (kobj_debug & D_RELOCATIONS) {
267 			_kobj_printf(ops, "krtld:\t\t\t\t0x%8llx",
268 			    (u_longlong_t)off);
269 			_kobj_printf(ops, " 0x%8llx\n", (u_longlong_t)value);
270 		}
271 #endif
272 		if (do_reloc_krtld(rtype, (unsigned char *)off, (Xword *)&value,
273 		    (const char *)mp->strings + symref->st_name,
274 		    mp->filename) == 0)
275 			err = 1;
276 	} /* end of while loop */
277 
278 	if (err)
279 		return (-1);
280 
281 	return (0);
282 }
283 
284 int
285 do_relocations(struct module *mp)
286 {
287 	uint_t shn;
288 	Shdr *shp, *rshp;
289 	uint_t nreloc;
290 
291 	/* do the relocations */
292 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
293 		rshp = (Shdr *)
294 		    (mp->shdrs + shn * mp->hdr.e_shentsize);
295 		if (rshp->sh_type == SHT_REL) {
296 			_kobj_printf(ops, "%s can't process type SHT_REL\n",
297 			    mp->filename);
298 			return (-1);
299 		}
300 		if (rshp->sh_type != SHT_RELA)
301 			continue;
302 		if (rshp->sh_link != mp->symtbl_section) {
303 			_kobj_printf(ops, "%s reloc for non-default symtab\n",
304 			    mp->filename);
305 			return (-1);
306 		}
307 		if (rshp->sh_info >= mp->hdr.e_shnum) {
308 			_kobj_printf(ops, "do_relocations: %s ", mp->filename);
309 			_kobj_printf(ops, " sh_info out of range %d\n", shn);
310 			goto bad;
311 		}
312 		nreloc = rshp->sh_size / rshp->sh_entsize;
313 
314 		/* get the section header that this reloc table refers to */
315 		shp = (Shdr *)
316 		    (mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize);
317 		/*
318 		 * Do not relocate any section that isn't loaded into memory.
319 		 * Most commonly this will skip over the .rela.stab* sections
320 		 */
321 		if (!(shp->sh_flags & SHF_ALLOC))
322 			continue;
323 #ifdef	KOBJ_DEBUG
324 		if (kobj_debug & D_RELOCATIONS) {
325 			_kobj_printf(ops, "krtld: relocating: file=%s ",
326 			    mp->filename);
327 			_kobj_printf(ops, " section=%d\n", shn);
328 		}
329 #endif
330 		if (do_relocate(mp, (char *)rshp->sh_addr, nreloc,
331 		    rshp->sh_entsize, shp->sh_addr) < 0) {
332 			_kobj_printf(ops,
333 			    "do_relocations: %s do_relocate failed\n",
334 			    mp->filename);
335 			goto bad;
336 		}
337 		kobj_free((void *)rshp->sh_addr, rshp->sh_size);
338 		rshp->sh_addr = 0;
339 	}
340 	mp->flags |= KOBJ_RELOCATED;
341 	return (0);
342 bad:
343 	kobj_free((void *)rshp->sh_addr, rshp->sh_size);
344 	rshp->sh_addr = 0;
345 	return (-1);
346 }
347