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