xref: /titanic_50/usr/src/uts/sparc/krtld/kobj_reloc.c (revision 986fd29a0dc13f7608ef7f508f6e700bd7bc2720)
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 /*
23f3324781Sab196087  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * SPARC relocation code.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/user.h>
387c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
397c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
407c478bd9Sstevel@tonic-gate #include <sys/elf.h>
417c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
427c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
447c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
457c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
467c478bd9Sstevel@tonic-gate 
47*986fd29aSsetje #include "krtld/reloc.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Probe discovery support
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate #define	PROBE_MARKER_SYMBOL	"__tnf_probe_version_1"
547c478bd9Sstevel@tonic-gate #define	TAG_MARKER_SYMBOL	"__tnf_tag_version_1"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate extern int tnf_splice_probes(int, tnf_probe_control_t *, tnf_tag_data_t *);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * The kernel run-time linker calls this to try to resolve a reference
607c478bd9Sstevel@tonic-gate  * it can't otherwise resolve.  We see if it's marking a probe control
617c478bd9Sstevel@tonic-gate  * block or a probe tag block; if so, we do the resolution and return 0.
627c478bd9Sstevel@tonic-gate  * If not, we return 1 to show that we can't resolve it, either.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate static int
tnf_reloc_resolve(char * symname,Addr * value_p,Elf64_Sxword * addend_p,long offset,tnf_probe_control_t ** probelist,tnf_tag_data_t ** taglist)657c478bd9Sstevel@tonic-gate tnf_reloc_resolve(char *symname, Addr *value_p,
667c478bd9Sstevel@tonic-gate     Elf64_Sxword *addend_p,
677c478bd9Sstevel@tonic-gate     long offset,
687c478bd9Sstevel@tonic-gate     tnf_probe_control_t **probelist,
697c478bd9Sstevel@tonic-gate     tnf_tag_data_t **taglist)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	if (strcmp(symname, PROBE_MARKER_SYMBOL) == 0) {
727c478bd9Sstevel@tonic-gate 		*addend_p = 0;
737c478bd9Sstevel@tonic-gate 		((tnf_probe_control_t *)offset)->next = *probelist;
747c478bd9Sstevel@tonic-gate 		*probelist = (tnf_probe_control_t *)offset;
757c478bd9Sstevel@tonic-gate 		return (0);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	if (strcmp(symname, TAG_MARKER_SYMBOL) == 0) {
787c478bd9Sstevel@tonic-gate 		*addend_p = 0;
797c478bd9Sstevel@tonic-gate 		*value_p = (Addr)*taglist;
807c478bd9Sstevel@tonic-gate 		*taglist = (tnf_tag_data_t *)offset;
817c478bd9Sstevel@tonic-gate 		return (0);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	return (1);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	SDT_RESTORE_MASK	0xc1f80000
877c478bd9Sstevel@tonic-gate #define	SDT_RESTORE		0x81e80000
887c478bd9Sstevel@tonic-gate #define	SDT_NOP			0x01000000
897c478bd9Sstevel@tonic-gate #define	SDT_RET			0x81c7e008
907c478bd9Sstevel@tonic-gate #define	SDT_RETL		0x81c3e008
917c478bd9Sstevel@tonic-gate #define	SDT_RDO7_MASK		0xbf000000
927c478bd9Sstevel@tonic-gate #define	SDT_RDO7		0x9e000000
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static int
sdt_reloc_resolve(struct module * mp,char * symname,uint32_t * instr,long roff)957c478bd9Sstevel@tonic-gate sdt_reloc_resolve(struct module *mp, char *symname, uint32_t *instr, long roff)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	sdt_probedesc_t *sdp;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * The "statically defined tracing" (SDT) provider for DTrace uses
1017c478bd9Sstevel@tonic-gate 	 * a mechanism similar to TNF, but somewhat simpler.  (Surprise,
1027c478bd9Sstevel@tonic-gate 	 * surprise.)  The SDT mechanism works by replacing calls to the
1037c478bd9Sstevel@tonic-gate 	 * undefined routine __dtrace_probe_[name] with nop instructions.
1047c478bd9Sstevel@tonic-gate 	 * The relocations are logged, and SDT itself will later patch the
1057c478bd9Sstevel@tonic-gate 	 * running binary appropriately.
1067c478bd9Sstevel@tonic-gate 	 */
1077c478bd9Sstevel@tonic-gate 	if (strncmp(symname, sdt_prefix, strlen(sdt_prefix)) != 0)
1087c478bd9Sstevel@tonic-gate 		return (1);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	symname += strlen(sdt_prefix);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	sdp = kobj_alloc(sizeof (sdt_probedesc_t), KM_WAIT);
1137c478bd9Sstevel@tonic-gate 	sdp->sdpd_name = kobj_alloc(strlen(symname) + 1, KM_WAIT);
1147c478bd9Sstevel@tonic-gate 	bcopy(symname, sdp->sdpd_name, strlen(symname) + 1);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if ((uint32_t *)roff == instr) {
1177c478bd9Sstevel@tonic-gate 		/*
1187c478bd9Sstevel@tonic-gate 		 * This isn't an offset -- it's an absolute value.  (This is
1197c478bd9Sstevel@tonic-gate 		 * typically only true for "unix".)  We need to convert the
1207c478bd9Sstevel@tonic-gate 		 * value into an offset from mp->text.
1217c478bd9Sstevel@tonic-gate 		 */
1227c478bd9Sstevel@tonic-gate 		roff -= (uintptr_t)mp->text;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	sdp->sdpd_offset = roff;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	sdp->sdpd_next = mp->sdt_probes;
1287c478bd9Sstevel@tonic-gate 	mp->sdt_probes = sdp;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * If the next instruction is a restore (any variant), then the probe
1327c478bd9Sstevel@tonic-gate 	 * point is being tail-called.  Instead of patching the call to be a
1337c478bd9Sstevel@tonic-gate 	 * NOP, we must patch it to be a ret.  If the next instruction is
1347c478bd9Sstevel@tonic-gate 	 * writing to %o7, it must be a tail call from a leaf; we must patch
1357c478bd9Sstevel@tonic-gate 	 * the instruction to be a retl.
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 	if ((*(instr + 1) & SDT_RESTORE_MASK) == SDT_RESTORE) {
1387c478bd9Sstevel@tonic-gate 		*instr = SDT_RET;
1397c478bd9Sstevel@tonic-gate 	} else if ((*(instr + 1) & SDT_RDO7_MASK) == SDT_RDO7) {
1407c478bd9Sstevel@tonic-gate 		*instr = SDT_RETL;
1417c478bd9Sstevel@tonic-gate 	} else {
1427c478bd9Sstevel@tonic-gate 		*instr = SDT_NOP;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	return (0);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate int
1497c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
do_relocate(struct module * mp,char * reltbl,Word relshtype,int nreloc,int relocsize,Addr baseaddr)1507c478bd9Sstevel@tonic-gate do_relocate(
1517c478bd9Sstevel@tonic-gate 	struct module *mp,
1527c478bd9Sstevel@tonic-gate 	char *reltbl,
1537c478bd9Sstevel@tonic-gate 	Word relshtype,
1547c478bd9Sstevel@tonic-gate 	int nreloc,
1557c478bd9Sstevel@tonic-gate 	int relocsize,
1567c478bd9Sstevel@tonic-gate 	Addr baseaddr)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	Word stndx;
1597c478bd9Sstevel@tonic-gate 	long off, roff;
1607c478bd9Sstevel@tonic-gate 	uintptr_t reladdr, rend;
1617c478bd9Sstevel@tonic-gate 	uint_t rtype;
1627c478bd9Sstevel@tonic-gate 	Elf64_Sxword addend;
1637c478bd9Sstevel@tonic-gate 	Addr value, destination;
1647c478bd9Sstevel@tonic-gate 	Sym *symref;
1657c478bd9Sstevel@tonic-gate 	int symnum;
1667c478bd9Sstevel@tonic-gate 	int err = 0;
1677c478bd9Sstevel@tonic-gate 	tnf_probe_control_t *probelist = NULL;
1687c478bd9Sstevel@tonic-gate 	tnf_tag_data_t *taglist = NULL;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	reladdr = (uintptr_t)reltbl;
1717c478bd9Sstevel@tonic-gate 	rend = reladdr + nreloc * relocsize;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
1747c478bd9Sstevel@tonic-gate 	if (kobj_debug & D_RELOCATIONS) {
1757c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld:\ttype\t\t\toffset\t   addend"
1767c478bd9Sstevel@tonic-gate 		    "      symbol\n");
1777c478bd9Sstevel@tonic-gate 		_kobj_printf(ops, "krtld:\t\t\t\t\t   value\n");
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate #endif
1807c478bd9Sstevel@tonic-gate 	destination = baseaddr;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * If this machine is loading a module through an alternate address
1847c478bd9Sstevel@tonic-gate 	 * we need to compute the spot where the actual relocation will
1857c478bd9Sstevel@tonic-gate 	 * take place.
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 	if (mp->destination) {
1887c478bd9Sstevel@tonic-gate 		int i;
1897c478bd9Sstevel@tonic-gate 		Shdr * shp;
1907c478bd9Sstevel@tonic-gate 		shp = (Shdr *)mp->shdrs;
1917c478bd9Sstevel@tonic-gate 		for (i = 0; i < mp->hdr.e_shnum; i++, shp++) {
1927c478bd9Sstevel@tonic-gate 			if (shp->sh_addr == baseaddr) {
1937c478bd9Sstevel@tonic-gate 				if ((shp->sh_flags & SHF_ALLOC) &&
1947c478bd9Sstevel@tonic-gate 				    !(shp->sh_flags & SHF_WRITE))
1957c478bd9Sstevel@tonic-gate 					destination = (Addr)mp->destination +
1967c478bd9Sstevel@tonic-gate 					    (baseaddr - (Addr)mp->text);
1977c478bd9Sstevel@tonic-gate 				break;
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	symnum = -1;
2037c478bd9Sstevel@tonic-gate 	/* loop through relocations */
2047c478bd9Sstevel@tonic-gate 	while (reladdr < rend) {
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		symnum++;
2077c478bd9Sstevel@tonic-gate 		rtype = ELF_R_TYPE(((Rela *)reladdr)->r_info);
2087c478bd9Sstevel@tonic-gate 		roff = off = ((Rela *)reladdr)->r_offset;
2097c478bd9Sstevel@tonic-gate 		stndx = ELF_R_SYM(((Rela *)reladdr)->r_info);
2107c478bd9Sstevel@tonic-gate 		if (stndx >= mp->nsyms) {
2117c478bd9Sstevel@tonic-gate 			_kobj_printf(ops,
2127c478bd9Sstevel@tonic-gate 			    "do_relocate: bad strndx %d\n", symnum);
2137c478bd9Sstevel@tonic-gate 			return (-1);
2147c478bd9Sstevel@tonic-gate 		}
215d326b23bSrie 		if ((rtype > R_SPARC_NUM) || IS_TLS_INS(rtype)) {
2167c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: invalid relocation type %d",
2177c478bd9Sstevel@tonic-gate 			    rtype);
2187c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, " at 0x%llx:", off);
2197c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, " file=%s\n", mp->filename);
2207c478bd9Sstevel@tonic-gate 			err = 1;
2217c478bd9Sstevel@tonic-gate 			continue;
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 		addend = (long)(((Rela *)reladdr)->r_addend);
2247c478bd9Sstevel@tonic-gate 		reladdr += relocsize;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
2287c478bd9Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
2297c478bd9Sstevel@tonic-gate 			Sym *symp;
2307c478bd9Sstevel@tonic-gate 			symp = (Sym *)
2317c478bd9Sstevel@tonic-gate 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
2327c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "krtld:\t%s",
2335aefb655Srie 			    conv_reloc_SPARC_type(rtype));
2347c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "\t0x%8llx", off);
2357c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, " 0x%8llx", addend);
2367c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "  %s\n",
2377c478bd9Sstevel@tonic-gate 			    (const char *)mp->strings + symp->st_name);
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate #endif
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		if (rtype == R_SPARC_NONE)
2427c478bd9Sstevel@tonic-gate 			continue;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		if (!(mp->flags & KOBJ_EXEC))
2457c478bd9Sstevel@tonic-gate 			off += destination;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		/*
2487c478bd9Sstevel@tonic-gate 		 * if R_SPARC_RELATIVE, simply add base addr
2497c478bd9Sstevel@tonic-gate 		 * to reloc location
2507c478bd9Sstevel@tonic-gate 		 */
2517c478bd9Sstevel@tonic-gate 		if (rtype == R_SPARC_RELATIVE) {
2527c478bd9Sstevel@tonic-gate 			value = baseaddr;
2537c478bd9Sstevel@tonic-gate 		} else {
2547c478bd9Sstevel@tonic-gate 			/*
2557c478bd9Sstevel@tonic-gate 			 * get symbol table entry - if symbol is local
2567c478bd9Sstevel@tonic-gate 			 * value is base address of this object
2577c478bd9Sstevel@tonic-gate 			 */
2587c478bd9Sstevel@tonic-gate 			symref = (Sym *)
2597c478bd9Sstevel@tonic-gate 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
2607c478bd9Sstevel@tonic-gate 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
2617c478bd9Sstevel@tonic-gate 				/* *** this is different for .o and .so */
2627c478bd9Sstevel@tonic-gate 				value = symref->st_value;
2637c478bd9Sstevel@tonic-gate 			} else {
2647c478bd9Sstevel@tonic-gate 				/*
2657c478bd9Sstevel@tonic-gate 				 * It's global. Allow weak references.  If
2667c478bd9Sstevel@tonic-gate 				 * the symbol is undefined, give TNF (the
2677c478bd9Sstevel@tonic-gate 				 * kernel probes facility) a chance to see
2687c478bd9Sstevel@tonic-gate 				 * if it's a probe site, and fix it up if so.
2697c478bd9Sstevel@tonic-gate 				 */
2707c478bd9Sstevel@tonic-gate 				if (symref->st_shndx == SHN_UNDEF &&
2717c478bd9Sstevel@tonic-gate 				    sdt_reloc_resolve(mp, mp->strings +
2727c478bd9Sstevel@tonic-gate 				    symref->st_name, (uint32_t *)off,
2737c478bd9Sstevel@tonic-gate 				    roff + ((uintptr_t)baseaddr -
2747c478bd9Sstevel@tonic-gate 				    (uintptr_t)mp->text)) == 0)
2757c478bd9Sstevel@tonic-gate 					continue;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 				if (symref->st_shndx == SHN_UNDEF &&
2787c478bd9Sstevel@tonic-gate 				    tnf_reloc_resolve(mp->strings +
2797c478bd9Sstevel@tonic-gate 				    symref->st_name, &symref->st_value,
280f3324781Sab196087 				    &addend, off, &probelist, &taglist) != 0) {
2817c478bd9Sstevel@tonic-gate 					if (ELF_ST_BIND(symref->st_info)
2827c478bd9Sstevel@tonic-gate 					    != STB_WEAK) {
2837c478bd9Sstevel@tonic-gate 						_kobj_printf(ops,
2847c478bd9Sstevel@tonic-gate 						    "not found: %s\n",
2857c478bd9Sstevel@tonic-gate 						    mp->strings +
2867c478bd9Sstevel@tonic-gate 						    symref->st_name);
2877c478bd9Sstevel@tonic-gate 						err = 1;
2887c478bd9Sstevel@tonic-gate 					}
2897c478bd9Sstevel@tonic-gate 					continue;
2907c478bd9Sstevel@tonic-gate 				} else { /* symbol found  - relocate */
2917c478bd9Sstevel@tonic-gate 					/*
2927c478bd9Sstevel@tonic-gate 					 * calculate location of definition
2937c478bd9Sstevel@tonic-gate 					 * - symbol value plus base address of
2947c478bd9Sstevel@tonic-gate 					 * containing shared object
2957c478bd9Sstevel@tonic-gate 					 */
2967c478bd9Sstevel@tonic-gate 					value = symref->st_value;
2977c478bd9Sstevel@tonic-gate 				} /* end else symbol found */
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 		} /* end not R_SPARC_RELATIVE */
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		value += addend;
3027c478bd9Sstevel@tonic-gate 		if (IS_EXTOFFSET(rtype)) {
3037c478bd9Sstevel@tonic-gate 			value +=
3047c478bd9Sstevel@tonic-gate 			    (Word) ELF_R_TYPE_DATA(((Rela *)reladdr)->r_info);
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * calculate final value -
3097c478bd9Sstevel@tonic-gate 		 * if PC-relative, subtract ref addr
3107c478bd9Sstevel@tonic-gate 		 */
3117c478bd9Sstevel@tonic-gate 		if (IS_PC_RELATIVE(rtype)) {
3127c478bd9Sstevel@tonic-gate 			if (mp->destination)
3137c478bd9Sstevel@tonic-gate 				value -= (baseaddr + roff);
3147c478bd9Sstevel@tonic-gate 			else
3157c478bd9Sstevel@tonic-gate 				value -= off;
3167c478bd9Sstevel@tonic-gate 		}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
3197c478bd9Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
3207c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "krtld:\t\t\t\t0x%8llx", off);
3217c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, " 0x%8llx\n", value);
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate #endif
324f3324781Sab196087 		if (do_reloc_krtld(rtype, (unsigned char *)off, (Xword *)&value,
3257c478bd9Sstevel@tonic-gate 		    (const char *)mp->strings + symref->st_name,
326f3324781Sab196087 		    mp->filename) == 0)
3277c478bd9Sstevel@tonic-gate 			err = 1;
3287c478bd9Sstevel@tonic-gate 	} /* end of while loop */
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (err)
3317c478bd9Sstevel@tonic-gate 		return (-1);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (tnf_splice_probes(mp->flags & KOBJ_PRIM, probelist, taglist))
3347c478bd9Sstevel@tonic-gate 		mp->flags |= KOBJ_TNF_PROBE;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	return (0);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate int
do_relocations(struct module * mp)3407c478bd9Sstevel@tonic-gate do_relocations(struct module *mp)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	uint_t shn;
3437c478bd9Sstevel@tonic-gate 	Shdr *shp, *rshp;
3447c478bd9Sstevel@tonic-gate 	uint_t nreloc;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/* do the relocations */
3477c478bd9Sstevel@tonic-gate 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
3487c478bd9Sstevel@tonic-gate 		rshp = (Shdr *)
3497c478bd9Sstevel@tonic-gate 		    (mp->shdrs + shn * mp->hdr.e_shentsize);
3507c478bd9Sstevel@tonic-gate 		if (rshp->sh_type == SHT_REL) {
3517c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "%s can't process type SHT_REL\n",
3527c478bd9Sstevel@tonic-gate 			    mp->filename);
3537c478bd9Sstevel@tonic-gate 			return (-1);
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 		if (rshp->sh_type != SHT_RELA)
3567c478bd9Sstevel@tonic-gate 			continue;
3577c478bd9Sstevel@tonic-gate 		if (rshp->sh_link != mp->symtbl_section) {
3587c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "%s reloc for non-default symtab\n",
3597c478bd9Sstevel@tonic-gate 			    mp->filename);
3607c478bd9Sstevel@tonic-gate 			return (-1);
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		if (rshp->sh_info >= mp->hdr.e_shnum) {
3637c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "do_relocations: %s ", mp->filename);
3647c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, " sh_info out of range %lld\n", shn);
3657c478bd9Sstevel@tonic-gate 			goto bad;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 		nreloc = rshp->sh_size / rshp->sh_entsize;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 		/* get the section header that this reloc table refers to */
3707c478bd9Sstevel@tonic-gate 		shp = (Shdr *)
3717c478bd9Sstevel@tonic-gate 		    (mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize);
3727c478bd9Sstevel@tonic-gate 		/*
3737c478bd9Sstevel@tonic-gate 		 * Do not relocate any section that isn't loaded into memory.
3747c478bd9Sstevel@tonic-gate 		 * Most commonly this will skip over the .rela.stab* sections
3757c478bd9Sstevel@tonic-gate 		 */
3767c478bd9Sstevel@tonic-gate 		if (!(shp->sh_flags & SHF_ALLOC))
3777c478bd9Sstevel@tonic-gate 			continue;
3787c478bd9Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
3797c478bd9Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
3807c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: relocating: file=%s ",
3817c478bd9Sstevel@tonic-gate 			    mp->filename);
3827c478bd9Sstevel@tonic-gate 			_kobj_printf(ops, " section=%d\n", shn);
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate #endif
3857c478bd9Sstevel@tonic-gate 		if (do_relocate(mp, (char *)rshp->sh_addr, rshp->sh_type,
3867c478bd9Sstevel@tonic-gate 		    nreloc, rshp->sh_entsize, shp->sh_addr) < 0) {
3877c478bd9Sstevel@tonic-gate 			_kobj_printf(ops,
3887c478bd9Sstevel@tonic-gate 			    "do_relocations: %s do_relocate failed\n",
3897c478bd9Sstevel@tonic-gate 			    mp->filename);
3907c478bd9Sstevel@tonic-gate 			goto bad;
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 		kobj_free((void *)rshp->sh_addr, rshp->sh_size);
3937c478bd9Sstevel@tonic-gate 		rshp->sh_addr = 0;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 	mp->flags |= KOBJ_RELOCATED;
3967c478bd9Sstevel@tonic-gate 	return (0);
3977c478bd9Sstevel@tonic-gate bad:
3987c478bd9Sstevel@tonic-gate 	kobj_free((void *)rshp->sh_addr, rshp->sh_size);
3997c478bd9Sstevel@tonic-gate 	rshp->sh_addr = 0;
4007c478bd9Sstevel@tonic-gate 	return (-1);
4017c478bd9Sstevel@tonic-gate }
402