156072Smjacob/*- 256072Smjacob * Copyright (c) 2003 Jake Burkholder. 356072Smjacob * Copyright 1996-1998 John D. Polstra. 456072Smjacob * All rights reserved. 556072Smjacob * 656072Smjacob * Redistribution and use in source and binary forms, with or without 756072Smjacob * modification, are permitted provided that the following conditions 856072Smjacob * are met: 956072Smjacob * 1. Redistributions of source code must retain the above copyright 1056072Smjacob * notice, this list of conditions and the following disclaimer. 1156072Smjacob * 2. Redistributions in binary form must reproduce the above copyright 1256072Smjacob * notice, this list of conditions and the following disclaimer in the 1356072Smjacob * documentation and/or other materials provided with the distribution. 1456072Smjacob * 1556072Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1656072Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1756072Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1856072Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1956072Smjacob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2056072Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2156072Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2256072Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2356072Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2456072Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2556072Smjacob * SUCH DAMAGE. 2656072Smjacob * 2756072Smjacob * $FreeBSD$ 2856072Smjacob */ 2956072Smjacob 3056072Smjacob#include <sys/types.h> 3156072Smjacob#include <machine/elf.h> 3256072Smjacob 3356072Smjacob#include <err.h> 3456072Smjacob#include <errno.h> 3557580Smjacob 3657580Smjacob#include "ef.h" 3756072Smjacob 3857580Smjacob/* 39 * Apply relocations to the values we got from the file. `relbase' is the 40 * target relocation address of the section, and `dataoff' is the target 41 * relocation address of the data in `dest'. 42 */ 43int 44ef_reloc(struct elf_file *ef, const void *reldata, int reltype, Elf_Off relbase, 45 Elf_Off dataoff, size_t len, void *_dest) 46{ 47 Elf_Addr *where, addr, addend; 48 Elf_Size rtype, symidx; 49 const Elf_Rel *rel; 50 const Elf_Rela *rela; 51 char *dest = _dest; 52 53 switch (reltype) { 54 case EF_RELOC_REL: 55 rel = (const Elf_Rel *)reldata; 56 where = (Elf_Addr *)(dest + relbase + rel->r_offset - dataoff); 57 addend = 0; 58 rtype = ELF_R_TYPE(rel->r_info); 59 symidx = ELF_R_SYM(rel->r_info); 60 break; 61 case EF_RELOC_RELA: 62 rela = (const Elf_Rela *)reldata; 63 where = (Elf_Addr *)(dest + relbase + rela->r_offset - dataoff); 64 addend = rela->r_addend; 65 rtype = ELF_R_TYPE(rela->r_info); 66 symidx = ELF_R_SYM(rela->r_info); 67 break; 68 default: 69 return (EINVAL); 70 } 71 72 if ((char *)where < (char *)dest || (char *)where >= (char *)dest + len) 73 return (0); 74 75 if (reltype == EF_RELOC_REL) 76 addend = *where; 77 78 switch (rtype) { 79 case R_386_RELATIVE: /* A + B */ 80 addr = (Elf_Addr)addend + relbase; 81 *where = addr; 82 break; 83 case R_386_32: /* S + A - P */ 84 addr = EF_SYMADDR(ef, symidx); 85 addr += addend; 86 *where = addr; 87 break; 88 case R_386_GLOB_DAT: /* S */ 89 addr = EF_SYMADDR(ef, symidx); 90 *where = addr; 91 break; 92 default: 93 warnx("unhandled relocation type %d", (int)rtype); 94 } 95 return (0); 96} 97