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 * $FreeBSD$ 33c1bba444SJohn Baldwin */ 34c1bba444SJohn Baldwin 35c1bba444SJohn Baldwin #include <sys/types.h> 36c1bba444SJohn Baldwin #include <machine/elf.h> 37c1bba444SJohn Baldwin 38c1bba444SJohn Baldwin #include <err.h> 39c1bba444SJohn Baldwin #include <errno.h> 40c1bba444SJohn Baldwin 41c1bba444SJohn Baldwin #include "ef.h" 42c1bba444SJohn Baldwin 43c1bba444SJohn Baldwin /* 44c1bba444SJohn Baldwin * Apply relocations to the values we got from the file. `relbase' is the 45c1bba444SJohn Baldwin * target relocation address of the section, and `dataoff' is the target 46c1bba444SJohn Baldwin * relocation address of the data in `dest'. 47c1bba444SJohn Baldwin */ 48c1bba444SJohn Baldwin int 49c1bba444SJohn Baldwin ef_reloc(struct elf_file *ef, const void *reldata, int reltype, Elf_Off relbase, 50c1bba444SJohn Baldwin Elf_Off dataoff, size_t len, void *dest) 51c1bba444SJohn Baldwin { 52c1bba444SJohn Baldwin Elf_Addr *where, val; 53c1bba444SJohn Baldwin const Elf_Rel *rel; 54c1bba444SJohn Baldwin const Elf_Rela *rela; 55c1bba444SJohn Baldwin Elf_Addr addend, addr; 56c1bba444SJohn Baldwin Elf_Size rtype, symidx; 57c1bba444SJohn Baldwin 58c1bba444SJohn Baldwin switch (reltype) { 59c1bba444SJohn Baldwin case EF_RELOC_REL: 60c1bba444SJohn Baldwin rel = (const Elf_Rel *)reldata; 61c1bba444SJohn Baldwin where = (Elf_Addr *)((char *)dest + relbase + rel->r_offset - 62c1bba444SJohn Baldwin dataoff); 63c1bba444SJohn Baldwin addend = 0; 64c1bba444SJohn Baldwin rtype = ELF_R_TYPE(rel->r_info); 65c1bba444SJohn Baldwin symidx = ELF_R_SYM(rel->r_info); 66c1bba444SJohn Baldwin break; 67c1bba444SJohn Baldwin case EF_RELOC_RELA: 68c1bba444SJohn Baldwin rela = (const Elf_Rela *)reldata; 69c1bba444SJohn Baldwin where = (Elf_Addr *)((char *)dest + relbase + rela->r_offset - 70c1bba444SJohn Baldwin dataoff); 71c1bba444SJohn Baldwin addend = rela->r_addend; 72c1bba444SJohn Baldwin rtype = ELF_R_TYPE(rela->r_info); 73c1bba444SJohn Baldwin symidx = ELF_R_SYM(rela->r_info); 74c1bba444SJohn Baldwin break; 75c1bba444SJohn Baldwin default: 76c1bba444SJohn Baldwin return (EINVAL); 77c1bba444SJohn Baldwin } 78c1bba444SJohn Baldwin 79c1bba444SJohn Baldwin if ((char *)where < (char *)dest || (char *)where >= (char *)dest + len) 80c1bba444SJohn Baldwin return (0); 81c1bba444SJohn Baldwin 82c1bba444SJohn Baldwin if (reltype == EF_RELOC_REL) 83c1bba444SJohn Baldwin addend = *where; 84c1bba444SJohn Baldwin 85c1bba444SJohn Baldwin switch (rtype) { 86c1bba444SJohn Baldwin #ifdef __LP64__ 87c1bba444SJohn Baldwin case R_MIPS_64: /* S + A */ 88c1bba444SJohn Baldwin #else 89c1bba444SJohn Baldwin case R_MIPS_32: /* S + A */ 90c1bba444SJohn Baldwin #endif 91c1bba444SJohn Baldwin addr = EF_SYMADDR(ef, symidx); 92c1bba444SJohn Baldwin val = addr + addend; 93c1bba444SJohn Baldwin *where = val; 94c1bba444SJohn Baldwin break; 95c1bba444SJohn Baldwin default: 96c1bba444SJohn Baldwin warnx("unhandled relocation type %d", (int)rtype); 97c1bba444SJohn Baldwin } 98c1bba444SJohn Baldwin return (0); 99c1bba444SJohn Baldwin } 100