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