ef_i386.c revision 186826
1134362Siedowse/*-
2134362Siedowse * Copyright (c) 2003 Jake Burkholder.
3134362Siedowse * Copyright 1996-1998 John D. Polstra.
4134362Siedowse * All rights reserved.
5134362Siedowse *
6134362Siedowse * Redistribution and use in source and binary forms, with or without
7134362Siedowse * modification, are permitted provided that the following conditions
8134362Siedowse * are met:
9134362Siedowse * 1. Redistributions of source code must retain the above copyright
10134362Siedowse *    notice, this list of conditions and the following disclaimer.
11134362Siedowse * 2. Redistributions in binary form must reproduce the above copyright
12134362Siedowse *    notice, this list of conditions and the following disclaimer in the
13134362Siedowse *    documentation and/or other materials provided with the distribution.
14134362Siedowse *
15134362Siedowse * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16134362Siedowse * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17134362Siedowse * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18134362Siedowse * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19134362Siedowse * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20134362Siedowse * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21134362Siedowse * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22134362Siedowse * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23134362Siedowse * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24134362Siedowse * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25134362Siedowse * SUCH DAMAGE.
26134362Siedowse *
27134362Siedowse * $FreeBSD: head/usr.sbin/kldxref/ef_i386.c 186826 2009-01-06 14:21:17Z luigi $
28134362Siedowse */
29134362Siedowse
30134362Siedowse#include <sys/types.h>
31134362Siedowse#include <machine/elf.h>
32134362Siedowse
33134362Siedowse#include <err.h>
34134362Siedowse#include <errno.h>
35134362Siedowse#include <string.h>
36134362Siedowse
37134362Siedowse#include "ef.h"
38134362Siedowse
39134362Siedowse/*
40134450Siedowse * Apply relocations to the values we got from the file. `relbase' is the
41134450Siedowse * target relocation address of the section, and `dataoff' is the target
42134450Siedowse * relocation address of the data in `dest'.
43134362Siedowse */
44134362Siedowseint
45134450Siedowseef_reloc(struct elf_file *ef, const void *reldata, int reltype, Elf_Off relbase,
46186826Sluigi    Elf_Off dataoff, size_t len, void *_dest)
47134362Siedowse{
48134362Siedowse	Elf_Addr *where, addr, addend;
49153504Smarcel	Elf_Size rtype, symidx;
50134362Siedowse	const Elf_Rel *rel;
51134362Siedowse	const Elf_Rela *rela;
52186826Sluigi	char *dest = _dest;
53134362Siedowse
54134450Siedowse	switch (reltype) {
55134362Siedowse	case EF_RELOC_REL:
56134450Siedowse		rel = (const Elf_Rel *)reldata;
57134450Siedowse		where = (Elf_Addr *)(dest + relbase + rel->r_offset - dataoff);
58134450Siedowse		addend = 0;
59134362Siedowse		rtype = ELF_R_TYPE(rel->r_info);
60134362Siedowse		symidx = ELF_R_SYM(rel->r_info);
61134362Siedowse		break;
62134362Siedowse	case EF_RELOC_RELA:
63134450Siedowse		rela = (const Elf_Rela *)reldata;
64134450Siedowse		where = (Elf_Addr *)(dest + relbase + rela->r_offset - dataoff);
65134362Siedowse		addend = rela->r_addend;
66134362Siedowse		rtype = ELF_R_TYPE(rela->r_info);
67134362Siedowse		symidx = ELF_R_SYM(rela->r_info);
68134362Siedowse		break;
69134362Siedowse	default:
70134362Siedowse		return (EINVAL);
71134362Siedowse	}
72134362Siedowse
73134362Siedowse	if ((char *)where < (char *)dest || (char *)where >= (char *)dest + len)
74134362Siedowse		return (0);
75134362Siedowse
76134450Siedowse	if (reltype == EF_RELOC_REL)
77134362Siedowse		addend = *where;
78134362Siedowse
79134362Siedowse	switch (rtype) {
80134362Siedowse	case R_386_RELATIVE:	/* A + B */
81134450Siedowse		addr = (Elf_Addr)addend + relbase;
82134362Siedowse		*where = addr;
83134362Siedowse		break;
84134362Siedowse	case R_386_32:	/* S + A - P */
85134362Siedowse		addr = EF_SYMADDR(ef, symidx);
86134362Siedowse		addr += addend;
87134362Siedowse		*where = addr;
88134362Siedowse		break;
89134362Siedowse	case R_386_GLOB_DAT:	/* S */
90134362Siedowse		addr = EF_SYMADDR(ef, symidx);
91134362Siedowse		*where = addr;
92134362Siedowse		break;
93134362Siedowse	default:
94134362Siedowse		warnx("unhandled relocation type %d", (int)rtype);
95134362Siedowse	}
96134362Siedowse	return (0);
97134362Siedowse}
98