1 1.3 christos /* $NetBSD: rmreloc.c,v 1.3 2007/03/04 05:59:07 christos Exp $ */ 2 1.1 bjh21 3 1.1 bjh21 /* 4 1.1 bjh21 * Copyright 1996 John D. Polstra. 5 1.1 bjh21 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com> 6 1.1 bjh21 * Copyright 2002 Charles M. Hannum <root (at) ihack.net> 7 1.1 bjh21 * All rights reserved. 8 1.1 bjh21 * 9 1.1 bjh21 * Redistribution and use in source and binary forms, with or without 10 1.1 bjh21 * modification, are permitted provided that the following conditions 11 1.1 bjh21 * are met: 12 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright 13 1.1 bjh21 * notice, this list of conditions and the following disclaimer. 14 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the 16 1.1 bjh21 * documentation and/or other materials provided with the distribution. 17 1.1 bjh21 * 3. All advertising materials mentioning features or use of this software 18 1.1 bjh21 * must display the following acknowledgement: 19 1.1 bjh21 * This product includes software developed by John Polstra. 20 1.1 bjh21 * 4. The name of the author may not be used to endorse or promote products 21 1.1 bjh21 * derived from this software without specific prior written permission. 22 1.1 bjh21 * 23 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 1.1 bjh21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 1.1 bjh21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 1.1 bjh21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 1.1 bjh21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 1.1 bjh21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 1.1 bjh21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 1.1 bjh21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 1.1 bjh21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 1.1 bjh21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 1.1 bjh21 */ 34 1.1 bjh21 /* 35 1.1 bjh21 * rmreloc.c - relocate an ELFish RISC OS relocatable module. 36 1.1 bjh21 */ 37 1.1 bjh21 /* 38 1.1 bjh21 * This code is a heavily hacked version of parts of: 39 1.1 bjh21 * lib/libexec/ld.elf_so/headers.c 40 1.1 bjh21 * lib/libexec/ld.elf_so/arch/arm/mdreloc.c 41 1.1 bjh21 * 42 1.1 bjh21 * At present it only deals with DT_REL tables containing R_ARM_NONE 43 1.1 bjh21 * and R_ARM_RELATIVE relocations, because those are all that my 44 1.1 bjh21 * linker emits. More can be added as needed. Note that this has to 45 1.1 bjh21 * handle relocating already-relocated code, e.g. after *RMTidy, so 46 1.1 bjh21 * most relocations have to reference oldbase, which ld.elf_so just 47 1.1 bjh21 * assumes is zero. There may be a cleverer way to do this. 48 1.1 bjh21 */ 49 1.1 bjh21 50 1.1 bjh21 #include <sys/types.h> 51 1.1 bjh21 #include <sys/stdint.h> 52 1.1 bjh21 #include <lib/libsa/stand.h> 53 1.1 bjh21 #define ELFSIZE 32 54 1.1 bjh21 #include <sys/exec_elf.h> 55 1.1 bjh21 56 1.1 bjh21 #include <riscoscalls.h> 57 1.1 bjh21 58 1.3 christos os_error *relocate_self(Elf_Dyn *, void *, void *); 59 1.1 bjh21 60 1.1 bjh21 #define assert(x) /* nothing */ 61 1.1 bjh21 62 1.1 bjh21 /* 63 1.1 bjh21 * While relocating ourselves, we must not refer to any global variables. 64 1.1 bjh21 * This includes _DYNAMIC -- the startup code finds it for us and passes 65 1.1 bjh21 * it to us along with the base address of the module. 66 1.1 bjh21 */ 67 1.1 bjh21 68 1.1 bjh21 typedef struct { 69 1.3 christos void * relocbase; /* Reloc const = mapbase - *vaddrbase */ 70 1.1 bjh21 Elf_Dyn *dynamic; /* Dynamic section */ 71 1.1 bjh21 const Elf_Rel *rel; /* Relocation entries */ 72 1.1 bjh21 const Elf_Rel *rellim; /* Limit of Relocation entries */ 73 1.1 bjh21 } Obj_Entry; 74 1.1 bjh21 75 1.1 bjh21 #define rdbg(x) /* nothing */ 76 1.1 bjh21 77 1.1 bjh21 /* 78 1.1 bjh21 * It is possible for the compiler to emit relocations for unaligned data. 79 1.1 bjh21 * We handle this situation with these inlines. 80 1.1 bjh21 */ 81 1.1 bjh21 #define RELOC_ALIGNED_P(x) \ 82 1.1 bjh21 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0) 83 1.1 bjh21 84 1.1 bjh21 static inline Elf_Addr 85 1.1 bjh21 load_ptr(void *where) 86 1.1 bjh21 { 87 1.1 bjh21 Elf_Addr res; 88 1.1 bjh21 89 1.1 bjh21 memcpy(&res, where, sizeof(res)); 90 1.1 bjh21 91 1.1 bjh21 return (res); 92 1.1 bjh21 } 93 1.1 bjh21 94 1.1 bjh21 static inline void 95 1.1 bjh21 store_ptr(void *where, Elf_Addr val) 96 1.1 bjh21 { 97 1.1 bjh21 98 1.1 bjh21 memcpy(where, &val, sizeof(val)); 99 1.1 bjh21 } 100 1.1 bjh21 101 1.1 bjh21 static struct os_error bad_reloc = { 102 1.1 bjh21 0, "Unhandled ELF redirection" 103 1.1 bjh21 }; 104 1.1 bjh21 105 1.1 bjh21 os_error * 106 1.3 christos relocate_self(Elf_Dyn *dynamic, void *oldbase, void *newbase) 107 1.1 bjh21 { 108 1.1 bjh21 Elf_Dyn *dynp; 109 1.2 christos Obj_Entry o = { 0 }; 110 1.2 christos Obj_Entry *obj; 111 1.1 bjh21 const Elf_Rel *rel; 112 1.1 bjh21 Elf_Addr relsz = 0; 113 1.1 bjh21 114 1.1 bjh21 obj = &o; obj->dynamic = dynamic; obj->relocbase = newbase; 115 1.1 bjh21 116 1.1 bjh21 for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; ++dynp) { 117 1.1 bjh21 switch (dynp->d_tag) { 118 1.1 bjh21 case DT_REL: 119 1.1 bjh21 obj->rel = (const Elf_Rel *) 120 1.1 bjh21 (obj->relocbase + dynp->d_un.d_ptr); 121 1.1 bjh21 break; 122 1.1 bjh21 case DT_RELSZ: 123 1.1 bjh21 relsz = dynp->d_un.d_val; 124 1.1 bjh21 break; 125 1.1 bjh21 case DT_RELENT: 126 1.1 bjh21 assert(dynp->d_un.d_val == sizeof(Elf_Rel)); 127 1.1 bjh21 break; 128 1.1 bjh21 } 129 1.1 bjh21 } 130 1.1 bjh21 131 1.3 christos obj->rellim = (const Elf_Rel *)((void *)obj->rel + relsz); 132 1.1 bjh21 133 1.1 bjh21 for (rel = obj->rel; rel < obj->rellim; rel++) { 134 1.1 bjh21 Elf_Addr *where; 135 1.1 bjh21 Elf_Addr tmp; 136 1.1 bjh21 137 1.1 bjh21 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 138 1.1 bjh21 139 1.1 bjh21 switch (ELF_R_TYPE(rel->r_info)) { 140 1.1 bjh21 case R_TYPE(NONE): 141 1.1 bjh21 break; 142 1.1 bjh21 143 1.1 bjh21 case R_TYPE(RELATIVE): /* word32 B + A */ 144 1.1 bjh21 if (__predict_true(RELOC_ALIGNED_P(where))) { 145 1.1 bjh21 tmp = *where + (Elf_Addr)obj->relocbase - 146 1.1 bjh21 (Elf_Addr)oldbase; 147 1.1 bjh21 *where = tmp; 148 1.1 bjh21 } else { 149 1.1 bjh21 tmp = load_ptr(where) + 150 1.1 bjh21 (Elf_Addr)obj->relocbase - 151 1.1 bjh21 (Elf_Addr)oldbase; 152 1.1 bjh21 store_ptr(where, tmp); 153 1.1 bjh21 } 154 1.1 bjh21 rdbg(("RELATIVE in %s --> %p", obj->path, 155 1.1 bjh21 (void *)tmp)); 156 1.1 bjh21 break; 157 1.1 bjh21 158 1.1 bjh21 default: 159 1.1 bjh21 return &bad_reloc; 160 1.1 bjh21 } 161 1.1 bjh21 } 162 1.1 bjh21 return NULL; 163 1.1 bjh21 } 164