1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003 Jake Burkholder.
5 * Copyright 1996-1998 John D. Polstra.
6 * All rights reserved.
7 * Copyright (c) 2023 Jessica Clarke <jrtc27@FreeBSD.org>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/endian.h>
32
33#include <err.h>
34#include <errno.h>
35#include <gelf.h>
36
37#include "ef.h"
38
39/*
40 * Apply relocations to the values obtained from the file. `relbase' is the
41 * target relocation address of the section, and `dataoff/len' is the region
42 * that is to be relocated, and has been copied to *dest
43 */
44static int
45ef_arm_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype,
46    GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest)
47{
48	char *where;
49	GElf_Addr addr, addend;
50	GElf_Size rtype, symidx;
51	const GElf_Rel *rel;
52	const GElf_Rela *rela;
53
54	switch (reltype) {
55	case ELF_T_REL:
56		rel = (const GElf_Rel *)reldata;
57		where = (char *)dest + (relbase + rel->r_offset - dataoff);
58		addend = 0;
59		rtype = GELF_R_TYPE(rel->r_info);
60		symidx = GELF_R_SYM(rel->r_info);
61		break;
62	case ELF_T_RELA:
63		rela = (const GElf_Rela *)reldata;
64		where = (char *)dest + (relbase + rela->r_offset - dataoff);
65		addend = rela->r_addend;
66		rtype = GELF_R_TYPE(rela->r_info);
67		symidx = GELF_R_SYM(rela->r_info);
68		break;
69	default:
70		return (EINVAL);
71	}
72
73	if (where < (char *)dest || where >= (char *)dest + len)
74		return (0);
75
76	if (reltype == ELF_T_REL)
77		addend = le32dec(where);
78
79	switch (rtype) {
80	case R_ARM_ABS32:	/* S + A */
81		addr = EF_SYMADDR(ef, symidx) + addend;
82		le32enc(where, addr);
83		break;
84	case R_ARM_RELATIVE:	/* B + A */
85		addr = relbase + addend;
86		le32enc(where, addr);
87		break;
88	default:
89		warnx("unhandled relocation type %d", (int)rtype);
90	}
91	return (0);
92}
93
94ELF_RELOC(ELFCLASS32, ELFDATA2LSB, EM_ARM, ef_arm_reloc);
95