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 /* 231618Srie * Copyright 2006 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 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 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 */ 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" 1440Sstevel@tonic-gate " 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 } 161*2145Srie 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 *) 1820Sstevel@tonic-gate (mp->symtbl+(stndx * mp->symhdr->sh_entsize)); 1830Sstevel@tonic-gate _kobj_printf(ops, "krtld:\t%s", 1841618Srie 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 *) 2080Sstevel@tonic-gate (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 + 2270Sstevel@tonic-gate symref->st_name, &symref->st_value, 2280Sstevel@tonic-gate &addend, 2290Sstevel@tonic-gate off, 2300Sstevel@tonic-gate &probelist, 2310Sstevel@tonic-gate &taglist) != 0) { 2320Sstevel@tonic-gate if (ELF_ST_BIND(symref->st_info) 2330Sstevel@tonic-gate != STB_WEAK) { 2340Sstevel@tonic-gate _kobj_printf(ops, 2350Sstevel@tonic-gate "not found: %s\n", 2360Sstevel@tonic-gate mp->strings + 2370Sstevel@tonic-gate symref->st_name); 2380Sstevel@tonic-gate err = 1; 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate continue; 2410Sstevel@tonic-gate } else { /* symbol found - relocate */ 2420Sstevel@tonic-gate /* 2430Sstevel@tonic-gate * calculate location of definition 2440Sstevel@tonic-gate * - symbol value plus base address of 2450Sstevel@tonic-gate * containing shared object 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate value = symref->st_value; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate } /* end else symbol found */ 2500Sstevel@tonic-gate } /* end global or weak */ 2510Sstevel@tonic-gate } /* end not R_AMD64_RELATIVE */ 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate value += addend; 2540Sstevel@tonic-gate /* 2550Sstevel@tonic-gate * calculate final value - 2560Sstevel@tonic-gate * if PC-relative, subtract ref addr 2570Sstevel@tonic-gate */ 2580Sstevel@tonic-gate if (IS_PC_RELATIVE(rtype)) 2590Sstevel@tonic-gate value -= off; 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate #ifdef KOBJ_DEBUG 2620Sstevel@tonic-gate if (kobj_debug & D_RELOCATIONS) { 2630Sstevel@tonic-gate _kobj_printf(ops, "krtld:\t\t\t\t0x%8llx", off); 2640Sstevel@tonic-gate _kobj_printf(ops, " 0x%8llx\n", value); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate #endif 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate if (do_reloc(rtype, (unsigned char *)off, &value, 2690Sstevel@tonic-gate (const char *)mp->strings + symref->st_name, 2701618Srie mp->filename, 0) == 0) 2710Sstevel@tonic-gate err = 1; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate } /* end of while loop */ 2740Sstevel@tonic-gate if (err) 2750Sstevel@tonic-gate return (-1); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate if (tnf_splice_probes(mp->flags & KOBJ_PRIM, probelist, taglist)) 2780Sstevel@tonic-gate mp->flags |= KOBJ_TNF_PROBE; 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate return (0); 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate int 2840Sstevel@tonic-gate do_relocations(struct module *mp) 2850Sstevel@tonic-gate { 2860Sstevel@tonic-gate uint_t shn; 2870Sstevel@tonic-gate Shdr *shp, *rshp; 2880Sstevel@tonic-gate uint_t nreloc; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate /* do the relocations */ 2910Sstevel@tonic-gate for (shn = 1; shn < mp->hdr.e_shnum; shn++) { 2920Sstevel@tonic-gate rshp = (Shdr *) 2930Sstevel@tonic-gate (mp->shdrs + shn * mp->hdr.e_shentsize); 2940Sstevel@tonic-gate if (rshp->sh_type == SHT_REL) { 2950Sstevel@tonic-gate _kobj_printf(ops, "%s can't process type SHT_REL\n", 2960Sstevel@tonic-gate mp->filename); 2970Sstevel@tonic-gate return (-1); 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate if (rshp->sh_type != SHT_RELA) 3000Sstevel@tonic-gate continue; 3010Sstevel@tonic-gate if (rshp->sh_link != mp->symtbl_section) { 3020Sstevel@tonic-gate _kobj_printf(ops, "%s reloc for non-default symtab\n", 3030Sstevel@tonic-gate mp->filename); 3040Sstevel@tonic-gate return (-1); 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate if (rshp->sh_info >= mp->hdr.e_shnum) { 3070Sstevel@tonic-gate _kobj_printf(ops, "do_relocations: %s sh_info ", 3080Sstevel@tonic-gate mp->filename); 3090Sstevel@tonic-gate _kobj_printf(ops, "out of range %d\n", shn); 3100Sstevel@tonic-gate goto bad; 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate nreloc = rshp->sh_size / rshp->sh_entsize; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* get the section header that this reloc table refers to */ 3150Sstevel@tonic-gate shp = (Shdr *) 3160Sstevel@tonic-gate (mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize); 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate /* 3190Sstevel@tonic-gate * Do not relocate any section that isn't loaded into memory. 3200Sstevel@tonic-gate * Most commonly this will skip over the .rela.stab* sections 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate if (!(shp->sh_flags & SHF_ALLOC)) 3230Sstevel@tonic-gate continue; 3240Sstevel@tonic-gate #ifdef KOBJ_DEBUG 3250Sstevel@tonic-gate if (kobj_debug & D_RELOCATIONS) { 3260Sstevel@tonic-gate _kobj_printf(ops, "krtld: relocating: file=%s ", 3270Sstevel@tonic-gate mp->filename); 3280Sstevel@tonic-gate _kobj_printf(ops, "section=%d\n", shn); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate #endif 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate if (do_relocate(mp, (char *)rshp->sh_addr, rshp->sh_type, 3330Sstevel@tonic-gate nreloc, rshp->sh_entsize, shp->sh_addr) < 0) { 3340Sstevel@tonic-gate _kobj_printf(ops, 3350Sstevel@tonic-gate "do_relocations: %s do_relocate failed\n", 3360Sstevel@tonic-gate mp->filename); 3370Sstevel@tonic-gate goto bad; 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate kobj_free((void *)rshp->sh_addr, rshp->sh_size); 3400Sstevel@tonic-gate rshp->sh_addr = 0; 3410Sstevel@tonic-gate } 3420Sstevel@tonic-gate mp->flags |= KOBJ_RELOCATED; 3430Sstevel@tonic-gate return (0); 3440Sstevel@tonic-gate bad: 3450Sstevel@tonic-gate kobj_free((void *)rshp->sh_addr, rshp->sh_size); 3460Sstevel@tonic-gate rshp->sh_addr = 0; 3470Sstevel@tonic-gate return (-1); 3480Sstevel@tonic-gate } 349