xref: /onnv-gate/usr/src/uts/intel/ia32/krtld/kobj_reloc.c (revision 5189:66a4f4f8a159)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211618Srie 
220Sstevel@tonic-gate /*
23*5189Sab196087  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * x86 relocation code.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/bootconf.h>
390Sstevel@tonic-gate #include <sys/modctl.h>
400Sstevel@tonic-gate #include <sys/elf.h>
410Sstevel@tonic-gate #include <sys/kobj.h>
420Sstevel@tonic-gate #include <sys/kobj_impl.h>
430Sstevel@tonic-gate #include <sys/tnf.h>
440Sstevel@tonic-gate #include <sys/tnf_probe.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include "reloc.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Probe Discovery
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	PROBE_MARKER_SYMBOL	"__tnf_probe_version_1"
540Sstevel@tonic-gate #define	TAG_MARKER_SYMBOL	"__tnf_tag_version_1"
550Sstevel@tonic-gate 
560Sstevel@tonic-gate extern int tnf_splice_probes(int, tnf_probe_control_t *, tnf_tag_data_t *);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * The kernel run-time linker calls this to try to resolve a reference
600Sstevel@tonic-gate  * it can't otherwise resolve.  We see if it's marking a probe control
610Sstevel@tonic-gate  * block; if so, we do the resolution and return 0.  If not, we return
620Sstevel@tonic-gate  * 1 to show that we can't resolve it, either.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate static int
tnf_reloc_resolve(char * symname,Addr * value_p,long offset,tnf_probe_control_t ** probelist,tnf_tag_data_t ** taglist)650Sstevel@tonic-gate tnf_reloc_resolve(char *symname,
660Sstevel@tonic-gate 	Addr *value_p,
670Sstevel@tonic-gate 	long offset,
680Sstevel@tonic-gate 	tnf_probe_control_t **probelist,
690Sstevel@tonic-gate 	tnf_tag_data_t **taglist)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	if (strcmp(symname, PROBE_MARKER_SYMBOL) == 0) {
720Sstevel@tonic-gate 		((tnf_probe_control_t *)offset)->next = *probelist;
730Sstevel@tonic-gate 		*probelist = (tnf_probe_control_t *)offset;
740Sstevel@tonic-gate 		return (0);
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 	if (strcmp(symname, TAG_MARKER_SYMBOL) == 0) {
770Sstevel@tonic-gate 		*value_p = (Addr)*taglist;
780Sstevel@tonic-gate 		*taglist = (tnf_tag_data_t *)offset;
790Sstevel@tonic-gate 		return (0);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	return (1);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	SDT_NOP		0x90
850Sstevel@tonic-gate #define	SDT_NOPS	5
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static int
sdt_reloc_resolve(struct module * mp,char * symname,uint8_t * instr)880Sstevel@tonic-gate sdt_reloc_resolve(struct module *mp, char *symname, uint8_t *instr)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	sdt_probedesc_t *sdp;
910Sstevel@tonic-gate 	int i;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/*
940Sstevel@tonic-gate 	 * The "statically defined tracing" (SDT) provider for DTrace uses
950Sstevel@tonic-gate 	 * a mechanism similar to TNF, but somewhat simpler.  (Surprise,
960Sstevel@tonic-gate 	 * surprise.)  The SDT mechanism works by replacing calls to the
970Sstevel@tonic-gate 	 * undefined routine __dtrace_probe_[name] with nop instructions.
980Sstevel@tonic-gate 	 * The relocations are logged, and SDT itself will later patch the
990Sstevel@tonic-gate 	 * running binary appropriately.
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 	if (strncmp(symname, sdt_prefix, strlen(sdt_prefix)) != 0)
1020Sstevel@tonic-gate 		return (1);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	symname += strlen(sdt_prefix);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	sdp = kobj_alloc(sizeof (sdt_probedesc_t), KM_WAIT);
1070Sstevel@tonic-gate 	sdp->sdpd_name = kobj_alloc(strlen(symname) + 1, KM_WAIT);
1080Sstevel@tonic-gate 	bcopy(symname, sdp->sdpd_name, strlen(symname) + 1);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	sdp->sdpd_offset = (uintptr_t)instr;
1110Sstevel@tonic-gate 	sdp->sdpd_next = mp->sdt_probes;
1120Sstevel@tonic-gate 	mp->sdt_probes = sdp;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	for (i = 0; i < SDT_NOPS; i++)
1150Sstevel@tonic-gate 		instr[i - 1] = SDT_NOP;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	return (0);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate int
1210Sstevel@tonic-gate /* ARGSUSED2 */
do_relocate(struct module * mp,char * reltbl,Word relshtype,int nreloc,int relocsize,Addr baseaddr)1220Sstevel@tonic-gate do_relocate(struct module *mp, char *reltbl, Word relshtype, int nreloc,
1230Sstevel@tonic-gate 	int relocsize, Addr baseaddr)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	unsigned long stndx;
1260Sstevel@tonic-gate 	unsigned long off;	/* can't be register for tnf_reloc_resolve() */
1270Sstevel@tonic-gate 	register unsigned long reladdr, rend;
1280Sstevel@tonic-gate 	register unsigned int rtype;
1290Sstevel@tonic-gate 	long value;
1300Sstevel@tonic-gate 	Sym *symref;
1310Sstevel@tonic-gate 	int err = 0;
1320Sstevel@tonic-gate 	tnf_probe_control_t *probelist = NULL;
1330Sstevel@tonic-gate 	tnf_tag_data_t *taglist = NULL;
1340Sstevel@tonic-gate 	int symnum;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	reladdr = (unsigned long)reltbl;
1370Sstevel@tonic-gate 	rend = reladdr + nreloc * relocsize;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
1400Sstevel@tonic-gate 	if (kobj_debug & D_RELOCATIONS) {
1410Sstevel@tonic-gate 		_kobj_printf(ops, "krtld:\ttype\t\t\toffset      symbol\n");
1420Sstevel@tonic-gate 		_kobj_printf(ops, "krtld:\t\t\t\t\t   value\n");
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate #endif
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	symnum = -1;
1470Sstevel@tonic-gate 	/* loop through relocations */
1480Sstevel@tonic-gate 	while (reladdr < rend) {
1490Sstevel@tonic-gate 		symnum++;
1500Sstevel@tonic-gate 		rtype = ELF32_R_TYPE(((Rel *)reladdr)->r_info);
1510Sstevel@tonic-gate 		off = ((Rel *)reladdr)->r_offset;
1520Sstevel@tonic-gate 		stndx = ELF32_R_SYM(((Rel *)reladdr)->r_info);
1530Sstevel@tonic-gate 		if (stndx >= mp->nsyms) {
1540Sstevel@tonic-gate 			_kobj_printf(ops, "do_relocate: bad strndx %d\n",
1550Sstevel@tonic-gate 			    symnum);
1560Sstevel@tonic-gate 			return (-1);
1570Sstevel@tonic-gate 		}
1582145Srie 		if ((rtype > R_386_NUM) || IS_TLS_INS(rtype)) {
1590Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: invalid relocation type %d",
1600Sstevel@tonic-gate 			    rtype);
1610Sstevel@tonic-gate 			_kobj_printf(ops, " at 0x%llx:", off);
1620Sstevel@tonic-gate 			_kobj_printf(ops, " file=%s\n", mp->filename);
1630Sstevel@tonic-gate 			err = 1;
1640Sstevel@tonic-gate 			continue;
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 		reladdr += relocsize;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		if (rtype == R_386_NONE)
1720Sstevel@tonic-gate 			continue;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
1750Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
1760Sstevel@tonic-gate 			Sym *	symp;
1770Sstevel@tonic-gate 			symp = (Sym *)
178*5189Sab196087 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
1790Sstevel@tonic-gate 			_kobj_printf(ops, "krtld:\t%s",
180*5189Sab196087 			    conv_reloc_386_type(rtype));
1810Sstevel@tonic-gate 			_kobj_printf(ops, "\t0x%8x", off);
1820Sstevel@tonic-gate 			_kobj_printf(ops, "  %s\n",
1830Sstevel@tonic-gate 			    (const char *)mp->strings + symp->st_name);
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate #endif
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		if (!(mp->flags & KOBJ_EXEC))
1880Sstevel@tonic-gate 			off += baseaddr;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		/*
1910Sstevel@tonic-gate 		 * if R_386_RELATIVE, simply add base addr
1920Sstevel@tonic-gate 		 * to reloc location
1930Sstevel@tonic-gate 		 */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		if (rtype == R_386_RELATIVE) {
1960Sstevel@tonic-gate 			value = baseaddr;
1970Sstevel@tonic-gate 		} else {
1980Sstevel@tonic-gate 			/*
1990Sstevel@tonic-gate 			 * get symbol table entry - if symbol is local
2000Sstevel@tonic-gate 			 * value is base address of this object
2010Sstevel@tonic-gate 			 */
2020Sstevel@tonic-gate 			symref = (Sym *)
203*5189Sab196087 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 			if (ELF32_ST_BIND(symref->st_info) == STB_LOCAL) {
2060Sstevel@tonic-gate 				/* *** this is different for .o and .so */
2070Sstevel@tonic-gate 				value = symref->st_value;
2080Sstevel@tonic-gate 			} else {
2090Sstevel@tonic-gate 				/*
2100Sstevel@tonic-gate 				 * It's global. Allow weak references.  If
2110Sstevel@tonic-gate 				 * the symbol is undefined, give TNF (the
2120Sstevel@tonic-gate 				 * kernel probes facility) a chance to see
2130Sstevel@tonic-gate 				 * if it's a probe site, and fix it up if so.
2140Sstevel@tonic-gate 				 */
2150Sstevel@tonic-gate 				if (symref->st_shndx == SHN_UNDEF &&
2160Sstevel@tonic-gate 				    sdt_reloc_resolve(mp, mp->strings +
2170Sstevel@tonic-gate 				    symref->st_name, (uint8_t *)off) == 0)
2180Sstevel@tonic-gate 					continue;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 				if (symref->st_shndx == SHN_UNDEF &&
2210Sstevel@tonic-gate 				    tnf_reloc_resolve(mp->strings +
222*5189Sab196087 				    symref->st_name, &symref->st_value,
223*5189Sab196087 				    off, &probelist, &taglist) != 0) {
2240Sstevel@tonic-gate 					if (ELF32_ST_BIND(symref->st_info)
2250Sstevel@tonic-gate 					    != STB_WEAK) {
2260Sstevel@tonic-gate 						_kobj_printf(ops,
2270Sstevel@tonic-gate 						    "not found: %s\n",
2280Sstevel@tonic-gate 						    mp->strings +
2290Sstevel@tonic-gate 						    symref->st_name);
2300Sstevel@tonic-gate 						err = 1;
2310Sstevel@tonic-gate 					}
2320Sstevel@tonic-gate 					continue;
2330Sstevel@tonic-gate 				} else { /* symbol found  - relocate */
2340Sstevel@tonic-gate 					/*
2350Sstevel@tonic-gate 					 * calculate location of definition
2360Sstevel@tonic-gate 					 * - symbol value plus base address of
2370Sstevel@tonic-gate 					 * containing shared object
2380Sstevel@tonic-gate 					 */
2390Sstevel@tonic-gate 					value = symref->st_value;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 				} /* end else symbol found */
2420Sstevel@tonic-gate 			} /* end global or weak */
2430Sstevel@tonic-gate 		} /* end not R_386_RELATIVE */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		/*
2460Sstevel@tonic-gate 		 * calculate final value -
2470Sstevel@tonic-gate 		 * if PC-relative, subtract ref addr
2480Sstevel@tonic-gate 		 */
2490Sstevel@tonic-gate 		if (IS_PC_RELATIVE(rtype))
2500Sstevel@tonic-gate 			value -= off;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
2530Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
2540Sstevel@tonic-gate 			_kobj_printf(ops, "krtld:\t\t\t\t0x%8x", off);
2550Sstevel@tonic-gate 			_kobj_printf(ops, " 0x%8x\n", value);
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate #endif
2580Sstevel@tonic-gate 
259*5189Sab196087 		if (do_reloc_krtld(rtype, (unsigned char *)off, (Word *)&value,
2600Sstevel@tonic-gate 		    (const char *)mp->strings + symref->st_name,
261*5189Sab196087 		    mp->filename) == 0)
2620Sstevel@tonic-gate 			err = 1;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	} /* end of while loop */
2650Sstevel@tonic-gate 	if (err)
2660Sstevel@tonic-gate 		return (-1);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (tnf_splice_probes(mp->flags & KOBJ_PRIM, probelist, taglist))
2690Sstevel@tonic-gate 		mp->flags |= KOBJ_TNF_PROBE;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	return (0);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate int
do_relocations(struct module * mp)2750Sstevel@tonic-gate do_relocations(struct module *mp)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	uint_t shn;
2780Sstevel@tonic-gate 	Shdr *shp, *rshp;
2790Sstevel@tonic-gate 	uint_t nreloc;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/* do the relocations */
2820Sstevel@tonic-gate 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2830Sstevel@tonic-gate 		rshp = (Shdr *)
284*5189Sab196087 		    (mp->shdrs + shn * mp->hdr.e_shentsize);
2850Sstevel@tonic-gate 		if (rshp->sh_type == SHT_RELA) {
2860Sstevel@tonic-gate 			_kobj_printf(ops, "%s can't process type SHT_RELA\n",
2870Sstevel@tonic-gate 			    mp->filename);
2880Sstevel@tonic-gate 			return (-1);
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 		if (rshp->sh_type != SHT_REL)
2910Sstevel@tonic-gate 			continue;
2920Sstevel@tonic-gate 		if (rshp->sh_link != mp->symtbl_section) {
2930Sstevel@tonic-gate 			_kobj_printf(ops, "%s reloc for non-default symtab\n",
2940Sstevel@tonic-gate 			    mp->filename);
2950Sstevel@tonic-gate 			return (-1);
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 		if (rshp->sh_info >= mp->hdr.e_shnum) {
2980Sstevel@tonic-gate 			_kobj_printf(ops, "do_relocations: %s sh_info ",
2990Sstevel@tonic-gate 			    mp->filename);
3000Sstevel@tonic-gate 			_kobj_printf(ops, "out of range %d\n", shn);
3010Sstevel@tonic-gate 			goto bad;
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 		nreloc = rshp->sh_size / rshp->sh_entsize;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		/* get the section header that this reloc table refers to */
3060Sstevel@tonic-gate 		shp = (Shdr *)
3070Sstevel@tonic-gate 		    (mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		/*
3100Sstevel@tonic-gate 		 * Do not relocate any section that isn't loaded into memory.
3110Sstevel@tonic-gate 		 * Most commonly this will skip over the .rela.stab* sections
3120Sstevel@tonic-gate 		 */
3130Sstevel@tonic-gate 		if (!(shp->sh_flags & SHF_ALLOC))
3140Sstevel@tonic-gate 			continue;
3150Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
3160Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
3170Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: relocating: file=%s ",
3180Sstevel@tonic-gate 			    mp->filename);
3190Sstevel@tonic-gate 			_kobj_printf(ops, "section=%d\n", shn);
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate #endif
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		if (do_relocate(mp, (char *)rshp->sh_addr, rshp->sh_type,
3240Sstevel@tonic-gate 		    nreloc, rshp->sh_entsize, shp->sh_addr) < 0) {
3250Sstevel@tonic-gate 			_kobj_printf(ops,
3260Sstevel@tonic-gate 			    "do_relocations: %s do_relocate failed\n",
3270Sstevel@tonic-gate 			    mp->filename);
3280Sstevel@tonic-gate 			goto bad;
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 		kobj_free((void *)rshp->sh_addr, rshp->sh_size);
3310Sstevel@tonic-gate 		rshp->sh_addr = 0;
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	mp->flags |= KOBJ_RELOCATED;
3340Sstevel@tonic-gate 	return (0);
3350Sstevel@tonic-gate bad:
3360Sstevel@tonic-gate 	kobj_free((void *)rshp->sh_addr, rshp->sh_size);
3370Sstevel@tonic-gate 	rshp->sh_addr = 0;
3380Sstevel@tonic-gate 	return (-1);
3390Sstevel@tonic-gate }
340