xref: /onnv-gate/usr/src/uts/intel/amd64/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,Elf64_Sxword * addend_p,long offset,tnf_probe_control_t ** probelist,tnf_tag_data_t ** taglist)650Sstevel@tonic-gate tnf_reloc_resolve(char *symname, Addr *value_p,
660Sstevel@tonic-gate     Elf64_Sxword *addend_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 		*addend_p = 0;
730Sstevel@tonic-gate 		((tnf_probe_control_t *)offset)->next = *probelist;
740Sstevel@tonic-gate 		*probelist = (tnf_probe_control_t *)offset;
750Sstevel@tonic-gate 		return (0);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 	if (strcmp(symname, TAG_MARKER_SYMBOL) == 0) {
780Sstevel@tonic-gate 		*addend_p = 0;
790Sstevel@tonic-gate 		*value_p = (Addr)*taglist;
800Sstevel@tonic-gate 		*taglist = (tnf_tag_data_t *)offset;
810Sstevel@tonic-gate 		return (0);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 	return (1);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #define	SDT_NOP		0x90
870Sstevel@tonic-gate #define	SDT_NOPS	5
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static int
sdt_reloc_resolve(struct module * mp,char * symname,uint8_t * instr)900Sstevel@tonic-gate sdt_reloc_resolve(struct module *mp, char *symname, uint8_t *instr)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	sdt_probedesc_t *sdp;
930Sstevel@tonic-gate 	int i;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	/*
960Sstevel@tonic-gate 	 * The "statically defined tracing" (SDT) provider for DTrace uses
970Sstevel@tonic-gate 	 * a mechanism similar to TNF, but somewhat simpler.  (Surprise,
980Sstevel@tonic-gate 	 * surprise.)  The SDT mechanism works by replacing calls to the
990Sstevel@tonic-gate 	 * undefined routine __dtrace_probe_[name] with nop instructions.
1000Sstevel@tonic-gate 	 * The relocations are logged, and SDT itself will later patch the
1010Sstevel@tonic-gate 	 * running binary appropriately.
1020Sstevel@tonic-gate 	 */
1030Sstevel@tonic-gate 	if (strncmp(symname, sdt_prefix, strlen(sdt_prefix)) != 0)
1040Sstevel@tonic-gate 		return (1);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	symname += strlen(sdt_prefix);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	sdp = kobj_alloc(sizeof (sdt_probedesc_t), KM_WAIT);
1090Sstevel@tonic-gate 	sdp->sdpd_name = kobj_alloc(strlen(symname) + 1, KM_WAIT);
1100Sstevel@tonic-gate 	bcopy(symname, sdp->sdpd_name, strlen(symname) + 1);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	sdp->sdpd_offset = (uintptr_t)instr;
1130Sstevel@tonic-gate 	sdp->sdpd_next = mp->sdt_probes;
1140Sstevel@tonic-gate 	mp->sdt_probes = sdp;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	for (i = 0; i < SDT_NOPS; i++)
1170Sstevel@tonic-gate 		instr[i - 1] = SDT_NOP;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	return (0);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate int
1230Sstevel@tonic-gate /* ARGSUSED2 */
do_relocate(struct module * mp,char * reltbl,Word relshtype,int nreloc,int relocsize,Addr baseaddr)1240Sstevel@tonic-gate do_relocate(struct module *mp, char *reltbl, Word relshtype, int nreloc,
1250Sstevel@tonic-gate 	int relocsize, Addr baseaddr)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	unsigned long stndx;
1280Sstevel@tonic-gate 	unsigned long off;	/* can't be register for tnf_reloc_resolve() */
1290Sstevel@tonic-gate 	register unsigned long reladdr, rend;
1300Sstevel@tonic-gate 	register unsigned int rtype;
1310Sstevel@tonic-gate 	unsigned long value;
1320Sstevel@tonic-gate 	Elf64_Sxword addend;
1330Sstevel@tonic-gate 	Sym *symref;
1340Sstevel@tonic-gate 	int err = 0;
1350Sstevel@tonic-gate 	tnf_probe_control_t *probelist = NULL;
1360Sstevel@tonic-gate 	tnf_tag_data_t *taglist = NULL;
1370Sstevel@tonic-gate 	int symnum;
1380Sstevel@tonic-gate 	reladdr = (unsigned long)reltbl;
1390Sstevel@tonic-gate 	rend = reladdr + nreloc * relocsize;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
1420Sstevel@tonic-gate 	if (kobj_debug & D_RELOCATIONS) {
1430Sstevel@tonic-gate 		_kobj_printf(ops, "krtld:\ttype\t\t\toffset\t   addend"
144*5189Sab196087 		    "      symbol\n");
1450Sstevel@tonic-gate 		_kobj_printf(ops, "krtld:\t\t\t\t\t   value\n");
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate #endif
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	symnum = -1;
1500Sstevel@tonic-gate 	/* loop through relocations */
1510Sstevel@tonic-gate 	while (reladdr < rend) {
1520Sstevel@tonic-gate 		symnum++;
1530Sstevel@tonic-gate 		rtype = ELF_R_TYPE(((Rela *)reladdr)->r_info);
1540Sstevel@tonic-gate 		off = ((Rela *)reladdr)->r_offset;
1550Sstevel@tonic-gate 		stndx = ELF_R_SYM(((Rela *)reladdr)->r_info);
1560Sstevel@tonic-gate 		if (stndx >= mp->nsyms) {
1570Sstevel@tonic-gate 			_kobj_printf(ops, "do_relocate: bad strndx %d\n",
1580Sstevel@tonic-gate 			    symnum);
1590Sstevel@tonic-gate 			return (-1);
1600Sstevel@tonic-gate 		}
1612145Srie 		if ((rtype > R_AMD64_NUM) || IS_TLS_INS(rtype)) {
1620Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: invalid relocation type %d",
1630Sstevel@tonic-gate 			    rtype);
1640Sstevel@tonic-gate 			_kobj_printf(ops, " at 0x%llx:", off);
1650Sstevel@tonic-gate 			_kobj_printf(ops, " file=%s\n", mp->filename);
1660Sstevel@tonic-gate 			err = 1;
1670Sstevel@tonic-gate 			continue;
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		addend = (long)(((Rela *)reladdr)->r_addend);
1720Sstevel@tonic-gate 		reladdr += relocsize;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		if (rtype == R_AMD64_NONE)
1760Sstevel@tonic-gate 			continue;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
1790Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
1800Sstevel@tonic-gate 			Sym *	symp;
1810Sstevel@tonic-gate 			symp = (Sym *)
182*5189Sab196087 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
1830Sstevel@tonic-gate 			_kobj_printf(ops, "krtld:\t%s",
184*5189Sab196087 			    conv_reloc_amd64_type(rtype));
1850Sstevel@tonic-gate 			_kobj_printf(ops, "\t0x%8llx", off);
1860Sstevel@tonic-gate 			_kobj_printf(ops, " 0x%8llx", addend);
1870Sstevel@tonic-gate 			_kobj_printf(ops, "  %s\n",
1880Sstevel@tonic-gate 			    (const char *)mp->strings + symp->st_name);
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate #endif
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		if (!(mp->flags & KOBJ_EXEC))
1930Sstevel@tonic-gate 			off += baseaddr;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		/*
1960Sstevel@tonic-gate 		 * if R_AMD64_RELATIVE, simply add base addr
1970Sstevel@tonic-gate 		 * to reloc location
1980Sstevel@tonic-gate 		 */
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		if (rtype == R_AMD64_RELATIVE) {
2010Sstevel@tonic-gate 			value = baseaddr;
2020Sstevel@tonic-gate 		} else {
2030Sstevel@tonic-gate 			/*
2040Sstevel@tonic-gate 			 * get symbol table entry - if symbol is local
2050Sstevel@tonic-gate 			 * value is base address of this object
2060Sstevel@tonic-gate 			 */
2070Sstevel@tonic-gate 			symref = (Sym *)
208*5189Sab196087 			    (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 			if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) {
2110Sstevel@tonic-gate 				/* *** this is different for .o and .so */
2120Sstevel@tonic-gate 				value = symref->st_value;
2130Sstevel@tonic-gate 			} else {
2140Sstevel@tonic-gate 				/*
2150Sstevel@tonic-gate 				 * It's global. Allow weak references.  If
2160Sstevel@tonic-gate 				 * the symbol is undefined, give TNF (the
2170Sstevel@tonic-gate 				 * kernel probes facility) a chance to see
2180Sstevel@tonic-gate 				 * if it's a probe site, and fix it up if so.
2190Sstevel@tonic-gate 				 */
2200Sstevel@tonic-gate 				if (symref->st_shndx == SHN_UNDEF &&
2210Sstevel@tonic-gate 				    sdt_reloc_resolve(mp, mp->strings +
2220Sstevel@tonic-gate 				    symref->st_name, (uint8_t *)off) == 0)
2230Sstevel@tonic-gate 					continue;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 				if (symref->st_shndx == SHN_UNDEF &&
2260Sstevel@tonic-gate 				    tnf_reloc_resolve(mp->strings +
227*5189Sab196087 				    symref->st_name, &symref->st_value,
228*5189Sab196087 				    &addend, off, &probelist, &taglist) != 0) {
2290Sstevel@tonic-gate 					if (ELF_ST_BIND(symref->st_info)
2300Sstevel@tonic-gate 					    != STB_WEAK) {
2310Sstevel@tonic-gate 						_kobj_printf(ops,
2320Sstevel@tonic-gate 						    "not found: %s\n",
2330Sstevel@tonic-gate 						    mp->strings +
2340Sstevel@tonic-gate 						    symref->st_name);
2350Sstevel@tonic-gate 						err = 1;
2360Sstevel@tonic-gate 					}
2370Sstevel@tonic-gate 					continue;
2380Sstevel@tonic-gate 				} else { /* symbol found  - relocate */
2390Sstevel@tonic-gate 					/*
2400Sstevel@tonic-gate 					 * calculate location of definition
2410Sstevel@tonic-gate 					 * - symbol value plus base address of
2420Sstevel@tonic-gate 					 * containing shared object
2430Sstevel@tonic-gate 					 */
2440Sstevel@tonic-gate 					value = symref->st_value;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 				} /* end else symbol found */
2470Sstevel@tonic-gate 			} /* end global or weak */
2480Sstevel@tonic-gate 		} /* end not R_AMD64_RELATIVE */
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		value += addend;
2510Sstevel@tonic-gate 		/*
2520Sstevel@tonic-gate 		 * calculate final value -
2530Sstevel@tonic-gate 		 * if PC-relative, subtract ref addr
2540Sstevel@tonic-gate 		 */
2550Sstevel@tonic-gate 		if (IS_PC_RELATIVE(rtype))
2560Sstevel@tonic-gate 			value -= off;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
2590Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
2600Sstevel@tonic-gate 			_kobj_printf(ops, "krtld:\t\t\t\t0x%8llx", off);
2610Sstevel@tonic-gate 			_kobj_printf(ops, " 0x%8llx\n", value);
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate #endif
2640Sstevel@tonic-gate 
265*5189Sab196087 		if (do_reloc_krtld(rtype, (unsigned char *)off, &value,
2660Sstevel@tonic-gate 		    (const char *)mp->strings + symref->st_name,
267*5189Sab196087 		    mp->filename) == 0)
2680Sstevel@tonic-gate 			err = 1;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	} /* end of while loop */
2710Sstevel@tonic-gate 	if (err)
2720Sstevel@tonic-gate 		return (-1);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if (tnf_splice_probes(mp->flags & KOBJ_PRIM, probelist, taglist))
2750Sstevel@tonic-gate 		mp->flags |= KOBJ_TNF_PROBE;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	return (0);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate int
do_relocations(struct module * mp)2810Sstevel@tonic-gate do_relocations(struct module *mp)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	uint_t shn;
2840Sstevel@tonic-gate 	Shdr *shp, *rshp;
2850Sstevel@tonic-gate 	uint_t nreloc;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/* do the relocations */
2880Sstevel@tonic-gate 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
2890Sstevel@tonic-gate 		rshp = (Shdr *)
290*5189Sab196087 		    (mp->shdrs + shn * mp->hdr.e_shentsize);
2910Sstevel@tonic-gate 		if (rshp->sh_type == SHT_REL) {
2920Sstevel@tonic-gate 			_kobj_printf(ops, "%s can't process type SHT_REL\n",
2930Sstevel@tonic-gate 			    mp->filename);
2940Sstevel@tonic-gate 			return (-1);
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		if (rshp->sh_type != SHT_RELA)
2970Sstevel@tonic-gate 			continue;
2980Sstevel@tonic-gate 		if (rshp->sh_link != mp->symtbl_section) {
2990Sstevel@tonic-gate 			_kobj_printf(ops, "%s reloc for non-default symtab\n",
3000Sstevel@tonic-gate 			    mp->filename);
3010Sstevel@tonic-gate 			return (-1);
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 		if (rshp->sh_info >= mp->hdr.e_shnum) {
3040Sstevel@tonic-gate 			_kobj_printf(ops, "do_relocations: %s sh_info ",
3050Sstevel@tonic-gate 			    mp->filename);
3060Sstevel@tonic-gate 			_kobj_printf(ops, "out of range %d\n", shn);
3070Sstevel@tonic-gate 			goto bad;
3080Sstevel@tonic-gate 		}
3090Sstevel@tonic-gate 		nreloc = rshp->sh_size / rshp->sh_entsize;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 		/* get the section header that this reloc table refers to */
3120Sstevel@tonic-gate 		shp = (Shdr *)
3130Sstevel@tonic-gate 		    (mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		/*
3160Sstevel@tonic-gate 		 * Do not relocate any section that isn't loaded into memory.
3170Sstevel@tonic-gate 		 * Most commonly this will skip over the .rela.stab* sections
3180Sstevel@tonic-gate 		 */
3190Sstevel@tonic-gate 		if (!(shp->sh_flags & SHF_ALLOC))
3200Sstevel@tonic-gate 			continue;
3210Sstevel@tonic-gate #ifdef	KOBJ_DEBUG
3220Sstevel@tonic-gate 		if (kobj_debug & D_RELOCATIONS) {
3230Sstevel@tonic-gate 			_kobj_printf(ops, "krtld: relocating: file=%s ",
3240Sstevel@tonic-gate 			    mp->filename);
3250Sstevel@tonic-gate 			_kobj_printf(ops, "section=%d\n", shn);
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate #endif
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 		if (do_relocate(mp, (char *)rshp->sh_addr, rshp->sh_type,
3300Sstevel@tonic-gate 		    nreloc, rshp->sh_entsize, shp->sh_addr) < 0) {
3310Sstevel@tonic-gate 			_kobj_printf(ops,
3320Sstevel@tonic-gate 			    "do_relocations: %s do_relocate failed\n",
3330Sstevel@tonic-gate 			    mp->filename);
3340Sstevel@tonic-gate 			goto bad;
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 		kobj_free((void *)rshp->sh_addr, rshp->sh_size);
3370Sstevel@tonic-gate 		rshp->sh_addr = 0;
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	mp->flags |= KOBJ_RELOCATED;
3400Sstevel@tonic-gate 	return (0);
3410Sstevel@tonic-gate bad:
3420Sstevel@tonic-gate 	kobj_free((void *)rshp->sh_addr, rshp->sh_size);
3430Sstevel@tonic-gate 	rshp->sh_addr = 0;
3440Sstevel@tonic-gate 	return (-1);
3450Sstevel@tonic-gate }
346