1c1bba444SJohn Baldwin /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3c1bba444SJohn Baldwin * 4c1bba444SJohn Baldwin * Copyright (c) 2019 John Baldwin <jhb@FreeBSD.org> 5c1bba444SJohn Baldwin * 6c1bba444SJohn Baldwin * This software was developed by SRI International and the University of 7c1bba444SJohn Baldwin * Cambridge Computer Laboratory (Department of Computer Science and 8c1bba444SJohn Baldwin * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the 9c1bba444SJohn Baldwin * DARPA SSITH research programme. 10c1bba444SJohn Baldwin * 11c1bba444SJohn Baldwin * Redistribution and use in source and binary forms, with or without 12c1bba444SJohn Baldwin * modification, are permitted provided that the following conditions 13c1bba444SJohn Baldwin * are met: 14c1bba444SJohn Baldwin * 1. Redistributions of source code must retain the above copyright 15c1bba444SJohn Baldwin * notice, this list of conditions and the following disclaimer. 16c1bba444SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright 17c1bba444SJohn Baldwin * notice, this list of conditions and the following disclaimer in the 18c1bba444SJohn Baldwin * documentation and/or other materials provided with the distribution. 19c1bba444SJohn Baldwin * 20c1bba444SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21c1bba444SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22c1bba444SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23c1bba444SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24c1bba444SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25c1bba444SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26c1bba444SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27c1bba444SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28c1bba444SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29c1bba444SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30c1bba444SJohn Baldwin * SUCH DAMAGE. 31c1bba444SJohn Baldwin */ 32c1bba444SJohn Baldwin 330299afdfSJohn Baldwin #include <sys/endian.h> 34c1bba444SJohn Baldwin 35c1bba444SJohn Baldwin #include <err.h> 36c1bba444SJohn Baldwin #include <errno.h> 370299afdfSJohn Baldwin #include <gelf.h> 38c1bba444SJohn Baldwin 39c1bba444SJohn Baldwin #include "ef.h" 40c1bba444SJohn Baldwin 41c1bba444SJohn Baldwin /* 42c1bba444SJohn Baldwin * Apply relocations to the values we got from the file. `relbase' is the 43c1bba444SJohn Baldwin * target relocation address of the section, and `dataoff' is the target 44c1bba444SJohn Baldwin * relocation address of the data in `dest'. 45c1bba444SJohn Baldwin */ 460299afdfSJohn Baldwin static int 470299afdfSJohn Baldwin ef_mips_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, 480299afdfSJohn Baldwin GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest) 49c1bba444SJohn Baldwin { 500299afdfSJohn Baldwin char *where; 510299afdfSJohn Baldwin GElf_Addr val; 520299afdfSJohn Baldwin const GElf_Rel *rel; 530299afdfSJohn Baldwin const GElf_Rela *rela; 540299afdfSJohn Baldwin GElf_Addr addend, addr; 550299afdfSJohn Baldwin GElf_Size rtype, symidx; 56c1bba444SJohn Baldwin 57c1bba444SJohn Baldwin switch (reltype) { 580299afdfSJohn Baldwin case ELF_T_REL: 590299afdfSJohn Baldwin rel = (const GElf_Rel *)reldata; 600299afdfSJohn Baldwin where = (char *)dest + relbase + rel->r_offset - dataoff; 61c1bba444SJohn Baldwin addend = 0; 620299afdfSJohn Baldwin rtype = GELF_R_TYPE(rel->r_info); 630299afdfSJohn Baldwin symidx = GELF_R_SYM(rel->r_info); 64c1bba444SJohn Baldwin break; 650299afdfSJohn Baldwin case ELF_T_RELA: 660299afdfSJohn Baldwin rela = (const GElf_Rela *)reldata; 670299afdfSJohn Baldwin where = (char *)dest + relbase + rela->r_offset - dataoff; 68c1bba444SJohn Baldwin addend = rela->r_addend; 690299afdfSJohn Baldwin rtype = GELF_R_TYPE(rela->r_info); 700299afdfSJohn Baldwin symidx = GELF_R_SYM(rela->r_info); 71c1bba444SJohn Baldwin break; 72c1bba444SJohn Baldwin default: 73c1bba444SJohn Baldwin return (EINVAL); 74c1bba444SJohn Baldwin } 75c1bba444SJohn Baldwin 760299afdfSJohn Baldwin if (where < (char *)dest || where >= (char *)dest + len) 77c1bba444SJohn Baldwin return (0); 78c1bba444SJohn Baldwin 790299afdfSJohn Baldwin if (reltype == ELF_T_REL) { 800299afdfSJohn Baldwin if (elf_class(ef) == ELFCLASS64) { 810299afdfSJohn Baldwin if (elf_encoding(ef) == ELFDATA2LSB) 820299afdfSJohn Baldwin addend = le64dec(where); 830299afdfSJohn Baldwin else 840299afdfSJohn Baldwin addend = be64dec(where); 850299afdfSJohn Baldwin } else { 860299afdfSJohn Baldwin if (elf_encoding(ef) == ELFDATA2LSB) 870299afdfSJohn Baldwin addend = le32dec(where); 880299afdfSJohn Baldwin else 890299afdfSJohn Baldwin addend = be32dec(where); 900299afdfSJohn Baldwin } 91c1bba444SJohn Baldwin addend = *where; 920299afdfSJohn Baldwin } 93c1bba444SJohn Baldwin 94c1bba444SJohn Baldwin switch (rtype) { 95c1bba444SJohn Baldwin case R_MIPS_64: /* S + A */ 96c1bba444SJohn Baldwin addr = EF_SYMADDR(ef, symidx); 97c1bba444SJohn Baldwin val = addr + addend; 980299afdfSJohn Baldwin if (elf_encoding(ef) == ELFDATA2LSB) 990299afdfSJohn Baldwin le64enc(where, val); 1000299afdfSJohn Baldwin else 1010299afdfSJohn Baldwin be64enc(where, val); 1020299afdfSJohn Baldwin break; 1030299afdfSJohn Baldwin case R_MIPS_32: /* S + A */ 1040299afdfSJohn Baldwin addr = EF_SYMADDR(ef, symidx); 1050299afdfSJohn Baldwin val = addr + addend; 1060299afdfSJohn Baldwin if (elf_encoding(ef) == ELFDATA2LSB) 1070299afdfSJohn Baldwin le32enc(where, val); 1080299afdfSJohn Baldwin else 1090299afdfSJohn Baldwin be32enc(where, val); 110c1bba444SJohn Baldwin break; 111c1bba444SJohn Baldwin default: 112c1bba444SJohn Baldwin warnx("unhandled relocation type %d", (int)rtype); 113c1bba444SJohn Baldwin } 114c1bba444SJohn Baldwin return (0); 115c1bba444SJohn Baldwin } 1160299afdfSJohn Baldwin 1170299afdfSJohn Baldwin ELF_RELOC(ELFCLASS32, ELFDATA2LSB, EM_MIPS, ef_mips_reloc); 1180299afdfSJohn Baldwin ELF_RELOC(ELFCLASS32, ELFDATA2MSB, EM_MIPS, ef_mips_reloc); 1190299afdfSJohn Baldwin ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_MIPS, ef_mips_reloc); 1200299afdfSJohn Baldwin ELF_RELOC(ELFCLASS64, ELFDATA2MSB, EM_MIPS, ef_mips_reloc); 121