17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215aefb655Srie 
227c478bd9Sstevel@tonic-gate /*
23f3324781Sab  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * SPARC relocation code.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/user.h>
367c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
377c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/elf.h>
397c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
407c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h>
417c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
427c478bd9Sstevel@tonic-gate 
43986fd29aSsetje #include "krtld/reloc.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	SDT_RESTORE_MASK	0xc1f80000
467c478bd9Sstevel@tonic-gate #define	SDT_RESTORE		0x81e80000
477c478bd9Sstevel@tonic-gate #define	SDT_NOP			0x01000000
487c478bd9Sstevel@tonic-gate #define	SDT_RET			0x81c7e008
497c478bd9Sstevel@tonic-gate #define	SDT_RETL		0x81c3e008
507c478bd9Sstevel@tonic-gate #define	SDT_RDO7_MASK		0xbf000000
517c478bd9Sstevel@tonic-gate #define	SDT_RDO7		0x9e000000
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static int
sdt_reloc_resolve(struct module * mp,char * symname,uint32_t * instr,long roff)547c478bd9Sstevel@tonic-gate sdt_reloc_resolve(struct module *mp, char *symname, uint32_t *instr, long roff)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	sdt_probedesc_t *sdp;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	/*
59*2570281cSToomas Soome 	 * The "statically defined tracing" (SDT) provider for DTrace.
60*2570281cSToomas Soome 	 * The SDT mechanism works by replacing calls to the
617c478bd9Sstevel@tonic-gate 	 * undefined routine __dtrace_probe_[name] with nop instructions.
627c478bd9Sstevel@tonic-gate 	 * The relocations are logged, and SDT itself will later patch the
637c478bd9Sstevel@tonic-gate 	 * running binary appropriately.
647c478bd9Sstevel@tonic-gate 	 */
657c478bd9Sstevel@tonic-gate 	if (strncmp(symname, sdt_prefix, strlen(sdt_prefix)) != 0)
667c478bd9Sstevel@tonic-gate 		return (1);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	symname += strlen(sdt_prefix);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	sdp = kobj_alloc(sizeof (sdt_probedesc_t), KM_WAIT);
717c478bd9Sstevel@tonic-gate 	sdp->sdpd_name = kobj_alloc(strlen(symname) + 1, KM_WAIT);
727c478bd9Sstevel@tonic-gate 	bcopy(symname, sdp->sdpd_name, strlen(symname) + 1);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if ((uint32_t *)roff == instr) {
757c478bd9Sstevel@tonic-gate 		/*
767c478bd9Sstevel@tonic-gate 		 * This isn't an offset -- it's an absolute value.  (This is
777c478bd9Sstevel@tonic-gate 		 * typically only true for "unix".)  We need to convert the
787c478bd9Sstevel@tonic-gate 		 * value into an offset from mp->text.
797c478bd9Sstevel@tonic-gate 		 */
807c478bd9Sstevel@tonic-gate 		roff -= (uintptr_t)mp->text;
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	sdp->sdpd_offset = roff;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	sdp->sdpd_next = mp->sdt_probes;
867c478bd9Sstevel@tonic-gate 	mp->sdt_probes = sdp;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * If the next instruction is a restore (any variant), then the probe
907c478bd9Sstevel@tonic-gate 	 * point is being tail-called.  Instead of patching the call to be a
917c478bd9Sstevel@tonic-gate 	 * NOP, we must patch it to be a ret.  If the next instruction is
927c478bd9Sstevel@tonic-gate 	 * writing to %o7, it must be a tail call from a leaf; we must patch
937c478bd9Sstevel@tonic-gate 	 * the instruction to be a retl.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	if ((*(instr + 1) & SDT_RESTORE_MASK) == SDT_RESTORE) {
967c478bd9Sstevel@tonic-gate 		*instr = SDT_RET;
977c478bd9Sstevel@tonic-gate 	} else if ((*(instr + 1) & SDT_RDO7_MASK) == SDT_RDO7) {
987c478bd9Sstevel@tonic-gate 		*instr = SDT_RETL;
997c478bd9Sstevel@tonic-gate 	} else {
1007c478bd9Sstevel@tonic-gate 		*instr = SDT_NOP;
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	return (0);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate int
do_relocate(struct module * mp,char * reltbl,int nreloc,int relocsize,Addr baseaddr)107584b574aSToomas Soome do_relocate(struct module *mp, char *reltbl, int nreloc, int relocsize,
108584b574aSToomas Soome     Addr baseaddr)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	Word stndx;
1117c478bd9Sstevel@tonic-gate 	long off, roff;
1127c478bd9Sstevel@tonic-gate 	uintptr_t reladdr, rend;
1137c478bd9Sstevel@tonic-gate 	uint_t rtype;
1147c478bd9Sstevel@tonic-gate 	Elf64_Sxword addend;
1157c478bd9Sstevel@tonic-gate 	Addr value, destination;
1167c478bd9Sstevel@tonic-gate 	Sym *symref;
1177c478bd9Sstevel@tonic-gate 	int symnum;
1187c478bd9Sstevel@tonic-gate 	int err = 0;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	reladdr = (uintptr_t)reltbl;
1217c478bd9Sstevel@tonic-gate 	rend = reladdr + nreloc * relocsize;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
1247c478bd9Sstevel@tonic-gate 	if (kobj_debug & D_RELOCATIONS) {
1257c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld:\ttype\t\t\toffset\t   addend"
126f3324781Sab 		    "      symbol\n");
1277c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld:\t\t\t\t\t   value\n");
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate 	destination = baseaddr;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * If this machine is loading a module through an alternate address
1347c478bd9Sstevel@tonic-gate 	 * we need to compute the spot where the actual relocation will
1357c478bd9Sstevel@tonic-gate 	 * take place.
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 	if (mp->destination) {
1387c478bd9Sstevel@tonic-gate 		int i;
1397c478bd9Sstevel@tonic-gate 		Shdr * shp;
1407c478bd9Sstevel@tonic-gate 		shp = (Shdr *)mp->shdrs;
1417c478bd9Sstevel@tonic-gate 		for (i = 0; i < mp->hdr.e_shnum; i++, shp++) {
1427c478bd9Sstevel@tonic-gate 			if (shp->sh_addr == baseaddr) {
1437c478bd9Sstevel@tonic-gate 				if ((shp->sh_flags & SHF_ALLOC) &&
144f3324781Sab 				    !(shp->sh_flags & SHF_WRITE))
1457c478bd9Sstevel@tonic-gate 					destination = (Addr)mp->destination +
146f3324781Sab 					    (baseaddr - (Addr)mp->text);
1477c478bd9Sstevel@tonic-gate 				break;
1487c478bd9Sstevel@tonic-gate 			}
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	symnum = -1;
1537c478bd9Sstevel@tonic-gate 	/* loop through relocations */
1547c478bd9Sstevel@tonic-gate 	while (reladdr < rend) {
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		symnum++;
1577c478bd9Sstevel@tonic-gate 		rtype = ELF_R_TYPE(((Rela *)reladdr)->r_info);
1587c478bd9Sstevel@tonic-gate 		roff = off = ((Rela *)reladdr)->r_offset;
1597c478bd9Sstevel@tonic-gate 		stndx = ELF_R_SYM(((Rela *)reladdr)->r_info);
1607c478bd9Sstevel@tonic-gate 		if (stndx >= mp->nsyms) {
1617c478bd9Sstevel@tonic-gate 			_kobj_printf(ops,
1627c478bd9Sstevel@tonic-gate 			    "do_relocate: bad strndx %d\n", symnum);
1637c478bd9Sstevel@tonic-gate 			return (-1);
1647c478bd9Sstevel@tonic-gate 		}
165d326b23bSrie 		if ((rtype > R_SPARC_NUM) || IS_TLS_INS(rtype)) {
1667c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: invalid relocation type %d",
1677c478bd9Sstevel@tonic-gate 			    rtype);
1684859da61SToomas Soome 			_kobj_printf(ops, " at 0x%llx:", (u_longlong_t)off);
1697c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, " file=%s\n", mp->filename);
1707c478bd9Sstevel@tonic-gate 			err = 1;
1717c478bd9Sstevel@tonic-gate 			continue;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 		addend = (long)(((Rela *)reladdr)->r_addend);
1747c478bd9Sstevel@tonic-gate 		reladdr += relocsize;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
1787c478bd9Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
1797c478bd9Sstevel@tonic-gate 			Sym *symp;
1807c478bd9Sstevel@tonic-gate 			symp = (Sym *)
1817c478bd9Sstevel@tonic-gate 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
1827c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "krtld:\t%s",
1835aefb655Srie 			    conv_reloc_SPARC_type(rtype));
1844859da61SToomas Soome 			_kobj_printf(ops, "\t0x%8llx", (u_longlong_t)off);
1854859da61SToomas Soome 			_kobj_printf(ops, " 0x%8llx", (u_longlong_t)addend);
1867c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "  %s\n",
1877c478bd9Sstevel@tonic-gate 			    (const char *)mp->strings + symp->st_name);
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate #endif
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		if (rtype == R_SPARC_NONE)
1927c478bd9Sstevel@tonic-gate 			continue;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		if (!(mp->flags & KOBJ_EXEC))
1957c478bd9Sstevel@tonic-gate 			off += destination;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		/*
1987c478bd9Sstevel@tonic-gate 		 * if R_SPARC_RELATIVE, simply add base addr
1997c478bd9Sstevel@tonic-gate 		 * to reloc location
2007c478bd9Sstevel@tonic-gate 		 */
2017c478bd9Sstevel@tonic-gate 		if (rtype == R_SPARC_RELATIVE) {
2027c478bd9Sstevel@tonic-gate 			value = baseaddr;
2037c478bd9Sstevel@tonic-gate 		} else {
2047c478bd9Sstevel@tonic-gate 			/*
2057c478bd9Sstevel@tonic-gate 			 * get symbol table entry - if symbol is local
2067c478bd9Sstevel@tonic-gate 			 * value is base address of this object
2077c478bd9Sstevel@tonic-gate 			 */
2087c478bd9Sstevel@tonic-gate 			symref = (Sym *)
209f3324781Sab 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
2107c478bd9Sstevel@tonic-gate 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
2117c478bd9Sstevel@tonic-gate 				/* *** this is different for .o and .so */
2127c478bd9Sstevel@tonic-gate 				value = symref->st_value;
2137c478bd9Sstevel@tonic-gate 			} else {
2147c478bd9Sstevel@tonic-gate 				/*
2157c478bd9Sstevel@tonic-gate 				 * It's global. Allow weak references.  If
216*2570281cSToomas Soome 				 * the symbol is undefined, give dtrace
217*2570281cSToomas Soome 				 * a chance to see if it's a probe site,
218*2570281cSToomas Soome 				 * and fix it up if so.
2197c478bd9Sstevel@tonic-gate 				 */
2207c478bd9Sstevel@tonic-gate 				if (symref->st_shndx == SHN_UNDEF &&
2217c478bd9Sstevel@tonic-gate 				    sdt_reloc_resolve(mp, mp->strings +
2227c478bd9Sstevel@tonic-gate 				    symref->st_name, (uint32_t *)off,
2237c478bd9Sstevel@tonic-gate 				    roff + ((uintptr_t)baseaddr -
2247c478bd9Sstevel@tonic-gate 				    (uintptr_t)mp->text)) == 0)
2257c478bd9Sstevel@tonic-gate 					continue;
2267c478bd9Sstevel@tonic-gate 
227*2570281cSToomas Soome 				if (symref->st_shndx == SHN_UNDEF) {
2287c478bd9Sstevel@tonic-gate 					if (ELF_ST_BIND(symref->st_info)
2297c478bd9Sstevel@tonic-gate 					    != STB_WEAK) {
2307c478bd9Sstevel@tonic-gate 						_kobj_printf(ops,
2317c478bd9Sstevel@tonic-gate 						    "not found: %s\n",
2327c478bd9Sstevel@tonic-gate 						    mp->strings +
2337c478bd9Sstevel@tonic-gate 						    symref->st_name);
2347c478bd9Sstevel@tonic-gate 						err = 1;
2357c478bd9Sstevel@tonic-gate 					}
2367c478bd9Sstevel@tonic-gate 					continue;
2377c478bd9Sstevel@tonic-gate 				} else { /* symbol found  - relocate */
2387c478bd9Sstevel@tonic-gate 					/*
2397c478bd9Sstevel@tonic-gate 					 * calculate location of definition
2407c478bd9Sstevel@tonic-gate 					 * - symbol value plus base address of
2417c478bd9Sstevel@tonic-gate 					 * containing shared object
2427c478bd9Sstevel@tonic-gate 					 */
2437c478bd9Sstevel@tonic-gate 					value = symref->st_value;
2447c478bd9Sstevel@tonic-gate 				} /* end else symbol found */
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 		} /* end not R_SPARC_RELATIVE */
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		value += addend;
2497c478bd9Sstevel@tonic-gate 		if (IS_EXTOFFSET(rtype)) {
2507c478bd9Sstevel@tonic-gate 			value +=
251f3324781Sab 			    (Word) ELF_R_TYPE_DATA(((Rela *)reladdr)->r_info);
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		/*
2557c478bd9Sstevel@tonic-gate 		 * calculate final value -
2567c478bd9Sstevel@tonic-gate 		 * if PC-relative, subtract ref addr
2577c478bd9Sstevel@tonic-gate 		 */
2587c478bd9Sstevel@tonic-gate 		if (IS_PC_RELATIVE(rtype)) {
2597c478bd9Sstevel@tonic-gate 			if (mp->destination)
2607c478bd9Sstevel@tonic-gate 				value -= (baseaddr + roff);
2617c478bd9Sstevel@tonic-gate 			else
2627c478bd9Sstevel@tonic-gate 				value -= off;
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
2667c478bd9Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
2674859da61SToomas Soome 			_kobj_printf(ops, "krtld:\t\t\t\t0x%8llx",
2684859da61SToomas Soome 			    (u_longlong_t)off);
2694859da61SToomas Soome 			_kobj_printf(ops, " 0x%8llx\n", (u_longlong_t)value);
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate #endif
272f3324781Sab 		if (do_reloc_krtld(rtype, (unsigned char *)off, (Xword *)&value,
2737c478bd9Sstevel@tonic-gate 		    (const char *)mp->strings + symref->st_name,
274f3324781Sab 		    mp->filename) == 0)
2757c478bd9Sstevel@tonic-gate 			err = 1;
2767c478bd9Sstevel@tonic-gate 	} /* end of while loop */
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (err)
2797c478bd9Sstevel@tonic-gate 		return (-1);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	return (0);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate int
do_relocations(struct module * mp)2857c478bd9Sstevel@tonic-gate do_relocations(struct module *mp)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	uint_t shn;
2887c478bd9Sstevel@tonic-gate 	Shdr *shp, *rshp;
2897c478bd9Sstevel@tonic-gate 	uint_t nreloc;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* do the relocations */
2927c478bd9Sstevel@tonic-gate 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2937c478bd9Sstevel@tonic-gate 		rshp = (Shdr *)
294f3324781Sab 		    (mp->shdrs + shn * mp->hdr.e_shentsize);
2957c478bd9Sstevel@tonic-gate 		if (rshp->sh_type == SHT_REL) {
2967c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "%s can't process type SHT_REL\n",
2977c478bd9Sstevel@tonic-gate 			    mp->filename);
2987c478bd9Sstevel@tonic-gate 			return (-1);
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 		if (rshp->sh_type != SHT_RELA)
3017c478bd9Sstevel@tonic-gate 			continue;
3027c478bd9Sstevel@tonic-gate 		if (rshp->sh_link != mp->symtbl_section) {
3037c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "%s reloc for non-default symtab\n",
3047c478bd9Sstevel@tonic-gate 			    mp->filename);
3057c478bd9Sstevel@tonic-gate 			return (-1);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 		if (rshp->sh_info >= mp->hdr.e_shnum) {
3087c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "do_relocations: %s ", mp->filename);
3094859da61SToomas Soome 			_kobj_printf(ops, " sh_info out of range %d\n", shn);
3107c478bd9Sstevel@tonic-gate 			goto bad;
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 		nreloc = rshp->sh_size / rshp->sh_entsize;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		/* get the section header that this reloc table refers to */
3157c478bd9Sstevel@tonic-gate 		shp = (Shdr *)
3167c478bd9Sstevel@tonic-gate 		    (mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize);
3177c478bd9Sstevel@tonic-gate 		/*
3187c478bd9Sstevel@tonic-gate 		 * Do not relocate any section that isn't loaded into memory.
3197c478bd9Sstevel@tonic-gate 		 * Most commonly this will skip over the .rela.stab* sections
3207c478bd9Sstevel@tonic-gate 		 */
3217c478bd9Sstevel@tonic-gate 		if (!(shp->sh_flags & SHF_ALLOC))
3227c478bd9Sstevel@tonic-gate 			continue;
3237c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
3247c478bd9Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
3257c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: relocating: file=%s ",
326f3324781Sab 			    mp->filename);
3277c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, " section=%d\n", shn);
3287c478bd9Sstevel@tonic-gate 		}
3297c478bd9Sstevel@tonic-gate #endif
330584b574aSToomas Soome 		if (do_relocate(mp, (char *)rshp->sh_addr, nreloc,
331584b574aSToomas Soome 		    rshp->sh_entsize, shp->sh_addr) < 0) {
3327c478bd9Sstevel@tonic-gate 			_kobj_printf(ops,
3337c478bd9Sstevel@tonic-gate 			    "do_relocations: %s do_relocate failed\n",
3347c478bd9Sstevel@tonic-gate 			    mp->filename);
3357c478bd9Sstevel@tonic-gate 			goto bad;
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 		kobj_free((void *)rshp->sh_addr, rshp->sh_size);
3387c478bd9Sstevel@tonic-gate 		rshp->sh_addr = 0;
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 	mp->flags |= KOBJ_RELOCATED;
3417c478bd9Sstevel@tonic-gate 	return (0);
3427c478bd9Sstevel@tonic-gate bad:
3437c478bd9Sstevel@tonic-gate 	kobj_free((void *)rshp->sh_addr, rshp->sh_size);
3447c478bd9Sstevel@tonic-gate 	rshp->sh_addr = 0;
3457c478bd9Sstevel@tonic-gate 	return (-1);
3467c478bd9Sstevel@tonic-gate }
347