2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * x86 relocation code.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/param.h>
2N/A#include <sys/sysmacros.h>
2N/A#include <sys/systm.h>
2N/A#include <sys/user.h>
2N/A#include <sys/bootconf.h>
2N/A#include <sys/modctl.h>
2N/A#include <sys/elf.h>
2N/A#include <sys/kobj.h>
2N/A#include <sys/kobj_impl.h>
2N/A#include <sys/tnf.h>
2N/A#include <sys/tnf_probe.h>
2N/A
2N/A#include "reloc.h"
2N/A
2N/A
2N/A/*
2N/A * Probe Discovery
2N/A */
2N/A
2N/A#define PROBE_MARKER_SYMBOL "__tnf_probe_version_1"
2N/A#define TAG_MARKER_SYMBOL "__tnf_tag_version_1"
2N/A
2N/Aextern int tnf_splice_probes(int, tnf_probe_control_t *, tnf_tag_data_t *);
2N/A
2N/A/*
2N/A * The kernel run-time linker calls this to try to resolve a reference
2N/A * it can't otherwise resolve. We see if it's marking a probe control
2N/A * block; if so, we do the resolution and return 0. If not, we return
2N/A * 1 to show that we can't resolve it, either.
2N/A */
2N/Astatic int
2N/Atnf_reloc_resolve(char *symname,
2N/A Addr *value_p,
2N/A long offset,
2N/A tnf_probe_control_t **probelist,
2N/A tnf_tag_data_t **taglist)
2N/A{
2N/A if (strcmp(symname, PROBE_MARKER_SYMBOL) == 0) {
2N/A ((tnf_probe_control_t *)offset)->next = *probelist;
2N/A *probelist = (tnf_probe_control_t *)offset;
2N/A return (0);
2N/A }
2N/A if (strcmp(symname, TAG_MARKER_SYMBOL) == 0) {
2N/A *value_p = (Addr)*taglist;
2N/A *taglist = (tnf_tag_data_t *)offset;
2N/A return (0);
2N/A }
2N/A return (1);
2N/A}
2N/A
2N/A#define SDT_NOP 0x90
2N/A#define SDT_NOPS 5
2N/A
2N/Astatic int
2N/Asdt_reloc_resolve(struct module *mp, char *symname, uint8_t *instr)
2N/A{
2N/A sdt_probedesc_t *sdp;
2N/A int i;
2N/A
2N/A /*
2N/A * The "statically defined tracing" (SDT) provider for DTrace uses
2N/A * a mechanism similar to TNF, but somewhat simpler. (Surprise,
2N/A * surprise.) The SDT mechanism works by replacing calls to the
2N/A * undefined routine __dtrace_probe_[name] with nop instructions.
2N/A * The relocations are logged, and SDT itself will later patch the
2N/A * running binary appropriately.
2N/A */
2N/A if (strncmp(symname, sdt_prefix, strlen(sdt_prefix)) != 0)
2N/A return (1);
2N/A
2N/A symname += strlen(sdt_prefix);
2N/A
2N/A sdp = kobj_alloc(sizeof (sdt_probedesc_t), KM_WAIT);
2N/A sdp->sdpd_name = kobj_alloc(strlen(symname) + 1, KM_WAIT);
2N/A bcopy(symname, sdp->sdpd_name, strlen(symname) + 1);
2N/A
2N/A sdp->sdpd_offset = (uintptr_t)instr;
2N/A sdp->sdpd_next = mp->sdt_probes;
2N/A mp->sdt_probes = sdp;
2N/A
2N/A for (i = 0; i < SDT_NOPS; i++)
2N/A instr[i - 1] = SDT_NOP;
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/A/* ARGSUSED2 */
2N/Ado_relocate(struct module *mp, char *reltbl, Word relshtype, int nreloc,
2N/A int relocsize, Addr baseaddr)
2N/A{
2N/A unsigned long stndx;
2N/A unsigned long off; /* can't be register for tnf_reloc_resolve() */
2N/A register unsigned long reladdr, rend;
2N/A register unsigned int rtype;
2N/A long value;
2N/A Sym *symref;
2N/A int err = 0;
2N/A tnf_probe_control_t *probelist = NULL;
2N/A tnf_tag_data_t *taglist = NULL;
2N/A int symnum;
2N/A
2N/A reladdr = (unsigned long)reltbl;
2N/A rend = reladdr + nreloc * relocsize;
2N/A
2N/A#ifdef KOBJ_DEBUG
2N/A if (kobj_debug & D_RELOCATIONS) {
2N/A _kobj_printf(ops, "krtld:\ttype\t\t\toffset symbol\n");
2N/A _kobj_printf(ops, "krtld:\t\t\t\t\t value\n");
2N/A }
2N/A#endif
2N/A
2N/A symnum = -1;
2N/A /* loop through relocations */
2N/A while (reladdr < rend) {
2N/A symnum++;
2N/A rtype = ELF32_R_TYPE(((Rel *)reladdr)->r_info);
2N/A off = ((Rel *)reladdr)->r_offset;
2N/A stndx = ELF32_R_SYM(((Rel *)reladdr)->r_info);
2N/A if (stndx >= mp->nsyms) {
2N/A _kobj_printf(ops, "do_relocate: bad strndx %d\n",
2N/A symnum);
2N/A return (-1);
2N/A }
2N/A if ((rtype > R_386_NUM) || IS_TLS_INS(rtype)) {
2N/A _kobj_printf(ops, "krtld: invalid relocation type %d",
2N/A rtype);
2N/A _kobj_printf(ops, " at 0x%llx:", off);
2N/A _kobj_printf(ops, " file=%s\n", mp->filename);
2N/A err = 1;
2N/A continue;
2N/A }
2N/A
2N/A
2N/A reladdr += relocsize;
2N/A
2N/A
2N/A if (rtype == R_386_NONE)
2N/A continue;
2N/A
2N/A#ifdef KOBJ_DEBUG
2N/A if (kobj_debug & D_RELOCATIONS) {
2N/A Sym * symp;
2N/A symp = (Sym *)
2N/A (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
2N/A _kobj_printf(ops, "krtld:\t%s",
2N/A conv_reloc_386_type(rtype));
2N/A _kobj_printf(ops, "\t0x%8x", off);
2N/A _kobj_printf(ops, " %s\n",
2N/A (const char *)mp->strings + symp->st_name);
2N/A }
2N/A#endif
2N/A
2N/A if (!(mp->flags & KOBJ_EXEC))
2N/A off += baseaddr;
2N/A
2N/A /*
2N/A * if R_386_RELATIVE, simply add base addr
2N/A * to reloc location
2N/A */
2N/A
2N/A if (rtype == R_386_RELATIVE) {
2N/A value = baseaddr;
2N/A } else {
2N/A /*
2N/A * get symbol table entry - if symbol is local
2N/A * value is base address of this object
2N/A */
2N/A symref = (Sym *)
2N/A (mp->symtbl+(stndx * mp->symhdr->sh_entsize));
2N/A
2N/A if (ELF32_ST_BIND(symref->st_info) == STB_LOCAL) {
2N/A /* *** this is different for .o and .so */
2N/A value = symref->st_value;
2N/A } else {
2N/A /*
2N/A * It's global. Allow weak references. If
2N/A * the symbol is undefined, give TNF (the
2N/A * kernel probes facility) a chance to see
2N/A * if it's a probe site, and fix it up if so.
2N/A */
2N/A if (symref->st_shndx == SHN_UNDEF &&
2N/A sdt_reloc_resolve(mp, mp->strings +
2N/A symref->st_name, (uint8_t *)off) == 0)
2N/A continue;
2N/A
2N/A if (symref->st_shndx == SHN_UNDEF &&
2N/A tnf_reloc_resolve(mp->strings +
symref->st_name, &symref->st_value,
off, &probelist, &taglist) != 0) {
if (ELF32_ST_BIND(symref->st_info)
!= STB_WEAK) {
_kobj_printf(ops,
"not found: %s\n",
mp->strings +
symref->st_name);
err = 1;
}
continue;
} else { /* symbol found - relocate */
/*
* calculate location of definition
* - symbol value plus base address of
* containing shared object
*/
value = symref->st_value;
} /* end else symbol found */
} /* end global or weak */
} /* end not R_386_RELATIVE */
/*
* calculate final value -
* if PC-relative, subtract ref addr
*/
if (IS_PC_RELATIVE(rtype))
value -= off;
#ifdef KOBJ_DEBUG
if (kobj_debug & D_RELOCATIONS) {
_kobj_printf(ops, "krtld:\t\t\t\t0x%8x", off);
_kobj_printf(ops, " 0x%8x\n", value);
}
#endif
if (do_reloc_krtld(rtype, (unsigned char *)off, (Word *)&value,
(const char *)mp->strings + symref->st_name,
mp->filename) == 0)
err = 1;
} /* end of while loop */
if (err)
return (-1);
if (tnf_splice_probes(mp->flags & KOBJ_PRIM, probelist, taglist))
mp->flags |= KOBJ_TNF_PROBE;
return (0);
}
int
do_relocations(struct module *mp)
{
uint_t shn;
Shdr *shp, *rshp;
uint_t nreloc;
/* do the relocations */
for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
rshp = (Shdr *)
(mp->shdrs + shn * mp->hdr.e_shentsize);
if (rshp->sh_type == SHT_RELA) {
_kobj_printf(ops, "%s can't process type SHT_RELA\n",
mp->filename);
return (-1);
}
if (rshp->sh_type != SHT_REL)
continue;
if (rshp->sh_link != mp->symtbl_section) {
_kobj_printf(ops, "%s reloc for non-default symtab\n",
mp->filename);
return (-1);
}
if (rshp->sh_info >= mp->hdr.e_shnum) {
_kobj_printf(ops, "do_relocations: %s sh_info ",
mp->filename);
_kobj_printf(ops, "out of range %d\n", shn);
goto bad;
}
nreloc = rshp->sh_size / rshp->sh_entsize;
/* get the section header that this reloc table refers to */
shp = (Shdr *)
(mp->shdrs + rshp->sh_info * mp->hdr.e_shentsize);
/*
* Do not relocate any section that isn't loaded into memory.
* Most commonly this will skip over the .rela.stab* sections
*/
if (!(shp->sh_flags & SHF_ALLOC))
continue;
#ifdef KOBJ_DEBUG
if (kobj_debug & D_RELOCATIONS) {
_kobj_printf(ops, "krtld: relocating: file=%s ",
mp->filename);
_kobj_printf(ops, "section=%d\n", shn);
}
#endif
if (do_relocate(mp, (char *)rshp->sh_addr, rshp->sh_type,
nreloc, rshp->sh_entsize, shp->sh_addr) < 0) {
_kobj_printf(ops,
"do_relocations: %s do_relocate failed\n",
mp->filename);
goto bad;
}
kobj_free((void *)rshp->sh_addr, rshp->sh_size);
rshp->sh_addr = 0;
}
mp->flags |= KOBJ_RELOCATED;
return (0);
bad:
kobj_free((void *)rshp->sh_addr, rshp->sh_size);
rshp->sh_addr = 0;
return (-1);
}