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