xref: /titanic_52/usr/src/uts/intel/amd64/krtld/kobj_reloc.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
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  * x86 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 
46 #include "reloc.h"
47 
48 
49 /*
50  * Probe Discovery
51  */
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; if so, we do the resolution and return 0.  If not, we return
62  * 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_NOP		0x90
87 #define	SDT_NOPS	5
88 
89 static int
90 sdt_reloc_resolve(struct module *mp, char *symname, uint8_t *instr)
91 {
92 	sdt_probedesc_t *sdp;
93 	int i;
94 
95 	/*
96 	 * The "statically defined tracing" (SDT) provider for DTrace uses
97 	 * a mechanism similar to TNF, but somewhat simpler.  (Surprise,
98 	 * surprise.)  The SDT mechanism works by replacing calls to the
99 	 * undefined routine __dtrace_probe_[name] with nop instructions.
100 	 * The relocations are logged, and SDT itself will later patch the
101 	 * running binary appropriately.
102 	 */
103 	if (strncmp(symname, sdt_prefix, strlen(sdt_prefix)) != 0)
104 		return (1);
105 
106 	symname += strlen(sdt_prefix);
107 
108 	sdp = kobj_alloc(sizeof (sdt_probedesc_t), KM_WAIT);
109 	sdp->sdpd_name = kobj_alloc(strlen(symname) + 1, KM_WAIT);
110 	bcopy(symname, sdp->sdpd_name, strlen(symname) + 1);
111 
112 	sdp->sdpd_offset = (uintptr_t)instr;
113 	sdp->sdpd_next = mp->sdt_probes;
114 	mp->sdt_probes = sdp;
115 
116 	for (i = 0; i < SDT_NOPS; i++)
117 		instr[i - 1] = SDT_NOP;
118 
119 	return (0);
120 }
121 
122 int
123 /* ARGSUSED2 */
124 do_relocate(struct module *mp, char *reltbl, Word relshtype, int nreloc,
125 	int relocsize, Addr baseaddr)
126 {
127 	unsigned long stndx;
128 	unsigned long off;	/* can't be register for tnf_reloc_resolve() */
129 	register unsigned long reladdr, rend;
130 	register unsigned int rtype;
131 	unsigned long value;
132 	Elf64_Sxword addend;
133 	Sym *symref;
134 	int err = 0;
135 	tnf_probe_control_t *probelist = NULL;
136 	tnf_tag_data_t *taglist = NULL;
137 	int symnum;
138 	reladdr = (unsigned long)reltbl;
139 	rend = reladdr + nreloc * relocsize;
140 
141 #ifdef	KOBJ_DEBUG
142 	if (kobj_debug & D_RELOCATIONS) {
143 		_kobj_printf(ops, "krtld:\ttype\t\t\toffset\t   addend"
144 		    "      symbol\n");
145 		_kobj_printf(ops, "krtld:\t\t\t\t\t   value\n");
146 	}
147 #endif
148 
149 	symnum = -1;
150 	/* loop through relocations */
151 	while (reladdr < rend) {
152 		symnum++;
153 		rtype = ELF_R_TYPE(((Rela *)reladdr)->r_info);
154 		off = ((Rela *)reladdr)->r_offset;
155 		stndx = ELF_R_SYM(((Rela *)reladdr)->r_info);
156 		if (stndx >= mp->nsyms) {
157 			_kobj_printf(ops, "do_relocate: bad strndx %d\n",
158 			    symnum);
159 			return (-1);
160 		}
161 		if ((rtype > R_AMD64_NUM) || IS_TLS_INS(rtype)) {
162 			_kobj_printf(ops, "krtld: invalid relocation type %d",
163 			    rtype);
164 			_kobj_printf(ops, " at 0x%llx:", off);
165 			_kobj_printf(ops, " file=%s\n", mp->filename);
166 			err = 1;
167 			continue;
168 		}
169 
170 
171 		addend = (long)(((Rela *)reladdr)->r_addend);
172 		reladdr += relocsize;
173 
174 
175 		if (rtype == R_AMD64_NONE)
176 			continue;
177 
178 #ifdef	KOBJ_DEBUG
179 		if (kobj_debug & D_RELOCATIONS) {
180 			Sym *	symp;
181 			symp = (Sym *)
182 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
183 			_kobj_printf(ops, "krtld:\t%s",
184 			    conv_reloc_amd64_type(rtype));
185 			_kobj_printf(ops, "\t0x%8llx", off);
186 			_kobj_printf(ops, " 0x%8llx", addend);
187 			_kobj_printf(ops, "  %s\n",
188 			    (const char *)mp->strings + symp->st_name);
189 		}
190 #endif
191 
192 		if (!(mp->flags & KOBJ_EXEC))
193 			off += baseaddr;
194 
195 		/*
196 		 * if R_AMD64_RELATIVE, simply add base addr
197 		 * to reloc location
198 		 */
199 
200 		if (rtype == R_AMD64_RELATIVE) {
201 			value = baseaddr;
202 		} else {
203 			/*
204 			 * get symbol table entry - if symbol is local
205 			 * value is base address of this object
206 			 */
207 			symref = (Sym *)
208 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
209 
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 TNF (the
217 				 * kernel probes facility) a chance to see
218 				 * if it's a probe site, 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, (uint8_t *)off) == 0)
223 					continue;
224 
225 				if (symref->st_shndx == SHN_UNDEF &&
226 				    tnf_reloc_resolve(mp->strings +
227 				    symref->st_name, &symref->st_value,
228 				    &addend, off, &probelist, &taglist) != 0) {
229 					if (ELF_ST_BIND(symref->st_info)
230 					    != STB_WEAK) {
231 						_kobj_printf(ops,
232 						    "not found: %s\n",
233 						    mp->strings +
234 						    symref->st_name);
235 						err = 1;
236 					}
237 					continue;
238 				} else { /* symbol found  - relocate */
239 					/*
240 					 * calculate location of definition
241 					 * - symbol value plus base address of
242 					 * containing shared object
243 					 */
244 					value = symref->st_value;
245 
246 				} /* end else symbol found */
247 			} /* end global or weak */
248 		} /* end not R_AMD64_RELATIVE */
249 
250 		value += addend;
251 		/*
252 		 * calculate final value -
253 		 * if PC-relative, subtract ref addr
254 		 */
255 		if (IS_PC_RELATIVE(rtype))
256 			value -= off;
257 
258 #ifdef	KOBJ_DEBUG
259 		if (kobj_debug & D_RELOCATIONS) {
260 			_kobj_printf(ops, "krtld:\t\t\t\t0x%8llx", off);
261 			_kobj_printf(ops, " 0x%8llx\n", value);
262 		}
263 #endif
264 
265 		if (do_reloc_krtld(rtype, (unsigned char *)off, &value,
266 		    (const char *)mp->strings + symref->st_name,
267 		    mp->filename) == 0)
268 			err = 1;
269 
270 	} /* end of while loop */
271 	if (err)
272 		return (-1);
273 
274 	if (tnf_splice_probes(mp->flags & KOBJ_PRIM, probelist, taglist))
275 		mp->flags |= KOBJ_TNF_PROBE;
276 
277 	return (0);
278 }
279 
280 int
281 do_relocations(struct module *mp)
282 {
283 	uint_t shn;
284 	Shdr *shp, *rshp;
285 	uint_t nreloc;
286 
287 	/* do the relocations */
288 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
289 		rshp = (Shdr *)
290 		    (mp->shdrs + shn * mp->hdr.e_shentsize);
291 		if (rshp->sh_type == SHT_REL) {
292 			_kobj_printf(ops, "%s can't process type SHT_REL\n",
293 			    mp->filename);
294 			return (-1);
295 		}
296 		if (rshp->sh_type != SHT_RELA)
297 			continue;
298 		if (rshp->sh_link != mp->symtbl_section) {
299 			_kobj_printf(ops, "%s reloc for non-default symtab\n",
300 			    mp->filename);
301 			return (-1);
302 		}
303 		if (rshp->sh_info >= mp->hdr.e_shnum) {
304 			_kobj_printf(ops, "do_relocations: %s sh_info ",
305 			    mp->filename);
306 			_kobj_printf(ops, "out of range %d\n", shn);
307 			goto bad;
308 		}
309 		nreloc = rshp->sh_size / rshp->sh_entsize;
310 
311 		/* get the section header that this reloc table refers to */
312 		shp = (Shdr *)
313 		    (mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize);
314 
315 		/*
316 		 * Do not relocate any section that isn't loaded into memory.
317 		 * Most commonly this will skip over the .rela.stab* sections
318 		 */
319 		if (!(shp->sh_flags & SHF_ALLOC))
320 			continue;
321 #ifdef	KOBJ_DEBUG
322 		if (kobj_debug & D_RELOCATIONS) {
323 			_kobj_printf(ops, "krtld: relocating: file=%s ",
324 			    mp->filename);
325 			_kobj_printf(ops, "section=%d\n", shn);
326 		}
327 #endif
328 
329 		if (do_relocate(mp, (char *)rshp->sh_addr, rshp->sh_type,
330 		    nreloc, rshp->sh_entsize, shp->sh_addr) < 0) {
331 			_kobj_printf(ops,
332 			    "do_relocations: %s do_relocate failed\n",
333 			    mp->filename);
334 			goto bad;
335 		}
336 		kobj_free((void *)rshp->sh_addr, rshp->sh_size);
337 		rshp->sh_addr = 0;
338 	}
339 	mp->flags |= KOBJ_RELOCATED;
340 	return (0);
341 bad:
342 	kobj_free((void *)rshp->sh_addr, rshp->sh_size);
343 	rshp->sh_addr = 0;
344 	return (-1);
345 }
346