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