1109607Sjake/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4109607Sjake * Copyright (c) 2003 Jake Burkholder.
5109607Sjake * All rights reserved.
6109607Sjake *
7109607Sjake * Redistribution and use in source and binary forms, with or without
8109607Sjake * modification, are permitted provided that the following conditions
9109607Sjake * are met:
10109607Sjake * 1. Redistributions of source code must retain the above copyright
11109607Sjake *    notice, this list of conditions and the following disclaimer.
12109607Sjake * 2. Redistributions in binary form must reproduce the above copyright
13109607Sjake *    notice, this list of conditions and the following disclaimer in the
14109607Sjake *    documentation and/or other materials provided with the distribution.
15109607Sjake *
16109607Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17109607Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18109607Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19109607Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20109607Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21109607Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22109607Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23109607Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24109607Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25109607Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26109607Sjake * SUCH DAMAGE.
27109607Sjake *
28109607Sjake * $FreeBSD: stable/11/usr.sbin/kldxref/ef_sparc64.c 330449 2018-03-05 07:26:05Z eadler $
29109607Sjake */
30109607Sjake
31109607Sjake#include <sys/types.h>
32109607Sjake#include <machine/elf.h>
33109607Sjake
34109607Sjake#include <err.h>
35109607Sjake#include <string.h>
36109607Sjake
37109607Sjake#include "ef.h"
38109607Sjake
39109607Sjake/*
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'.
43109607Sjake */
44109607Sjakeint
45134450Siedowseef_reloc(struct elf_file *ef, const void *reldata, int reltype, Elf_Off relbase,
46134450Siedowse    Elf_Off dataoff, size_t len, void *dest)
47109607Sjake{
48109607Sjake	const Elf_Rela *a;
49153504Smarcel	Elf_Size w;
50109607Sjake
51134450Siedowse	switch (reltype) {
52134358Siedowse	case EF_RELOC_RELA:
53134450Siedowse		a = reldata;
54134450Siedowse		if (relbase + a->r_offset >= dataoff && relbase + a->r_offset <
55134450Siedowse		    dataoff + len) {
56109607Sjake			switch (ELF_R_TYPE(a->r_info)) {
57109607Sjake			case R_SPARC_RELATIVE:
58134450Siedowse				w = a->r_addend + relbase;
59134450Siedowse				memcpy((u_char *)dest + (relbase + a->r_offset -
60134450Siedowse				    dataoff), &w, sizeof(w));
61109607Sjake				break;
62109607Sjake			default:
63109607Sjake				warnx("unhandled relocation type %u",
64153503Smarcel				    (unsigned int)ELF_R_TYPE(a->r_info));
65109607Sjake				break;
66109607Sjake			}
67109607Sjake		}
68134358Siedowse		break;
69109607Sjake	}
70109607Sjake	return (0);
71109607Sjake}
72