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