1109607Sjake/*-
2109607Sjake * Copyright (c) 2003 Jake Burkholder.
3109607Sjake * All rights reserved.
4109607Sjake *
5109607Sjake * Redistribution and use in source and binary forms, with or without
6109607Sjake * modification, are permitted provided that the following conditions
7109607Sjake * are met:
8109607Sjake * 1. Redistributions of source code must retain the above copyright
9109607Sjake *    notice, this list of conditions and the following disclaimer.
10109607Sjake * 2. Redistributions in binary form must reproduce the above copyright
11109607Sjake *    notice, this list of conditions and the following disclaimer in the
12109607Sjake *    documentation and/or other materials provided with the distribution.
13109607Sjake *
14109607Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15109607Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16109607Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17109607Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18109607Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19109607Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20109607Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21109607Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22109607Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23109607Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24109607Sjake * SUCH DAMAGE.
25109607Sjake *
26109607Sjake * $FreeBSD$
27109607Sjake */
28109607Sjake
29109607Sjake#include <sys/types.h>
30109607Sjake#include <machine/elf.h>
31109607Sjake
32109607Sjake#include <err.h>
33109607Sjake#include <string.h>
34109607Sjake
35109607Sjake#include "ef.h"
36109607Sjake
37109607Sjake/*
38134450Siedowse * Apply relocations to the values we got from the file. `relbase' is the
39134450Siedowse * target relocation address of the section, and `dataoff' is the target
40134450Siedowse * relocation address of the data in `dest'.
41109607Sjake */
42109607Sjakeint
43134450Siedowseef_reloc(struct elf_file *ef, const void *reldata, int reltype, Elf_Off relbase,
44134450Siedowse    Elf_Off dataoff, size_t len, void *dest)
45109607Sjake{
46109607Sjake	const Elf_Rela *a;
47153504Smarcel	Elf_Size w;
48109607Sjake
49134450Siedowse	switch (reltype) {
50134358Siedowse	case EF_RELOC_RELA:
51134450Siedowse		a = reldata;
52134450Siedowse		if (relbase + a->r_offset >= dataoff && relbase + a->r_offset <
53134450Siedowse		    dataoff + len) {
54109607Sjake			switch (ELF_R_TYPE(a->r_info)) {
55109607Sjake			case R_SPARC_RELATIVE:
56134450Siedowse				w = a->r_addend + relbase;
57134450Siedowse				memcpy((u_char *)dest + (relbase + a->r_offset -
58134450Siedowse				    dataoff), &w, sizeof(w));
59109607Sjake				break;
60109607Sjake			default:
61109607Sjake				warnx("unhandled relocation type %u",
62153503Smarcel				    (unsigned int)ELF_R_TYPE(a->r_info));
63109607Sjake				break;
64109607Sjake			}
65109607Sjake		}
66134358Siedowse		break;
67109607Sjake	}
68109607Sjake	return (0);
69109607Sjake}
70