1/* $NetBSD: mdreloc.c,v 1.36 2011/04/12 16:40:04 matt Exp $ */ 2 3#include <sys/cdefs.h> 4#ifndef lint 5__RCSID("$NetBSD: mdreloc.c,v 1.36 2011/04/12 16:40:04 matt Exp $"); 6#endif /* not lint */ 7 8#include <sys/types.h> 9#include <string.h> 10 11#include "debug.h" 12#include "rtld.h" 13 14void _rtld_bind_start(void); 15void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr); 16caddr_t _rtld_bind(const Obj_Entry *, Elf_Word); 17 18void 19_rtld_setup_pltgot(const Obj_Entry *obj) 20{ 21 obj->pltgot[1] = (Elf_Addr) obj; 22 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 23} 24 25void 26_rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 27{ 28 const Elf_Rel *rel = 0, *rellim; 29 Elf_Addr relsz = 0; 30 Elf_Addr *where; 31 32 for (; dynp->d_tag != DT_NULL; dynp++) { 33 switch (dynp->d_tag) { 34 case DT_REL: 35 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr); 36 break; 37 case DT_RELSZ: 38 relsz = dynp->d_un.d_val; 39 break; 40 } 41 } 42 rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz); 43 for (; rel < rellim; rel++) { 44 where = (Elf_Addr *)(relocbase + rel->r_offset); 45 *where += (Elf_Addr)relocbase; 46 } 47} 48 49/* 50 * It is possible for the compiler to emit relocations for unaligned data. 51 * We handle this situation with these inlines. 52 */ 53#define RELOC_ALIGNED_P(x) \ 54 (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0) 55 56static inline Elf_Addr 57load_ptr(void *where) 58{ 59 Elf_Addr res; 60 61 memcpy(&res, where, sizeof(res)); 62 63 return (res); 64} 65 66static inline void 67store_ptr(void *where, Elf_Addr val) 68{ 69 70 memcpy(where, &val, sizeof(val)); 71} 72 73int 74_rtld_relocate_nonplt_objects(Obj_Entry *obj) 75{ 76 const Elf_Rel *rel; 77 78 for (rel = obj->rel; rel < obj->rellim; rel++) { 79 Elf_Addr *where; 80 const Elf_Sym *def; 81 const Obj_Entry *defobj; 82 Elf_Addr tmp; 83 unsigned long symnum; 84 85 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 86 symnum = ELF_R_SYM(rel->r_info); 87 88 switch (ELF_R_TYPE(rel->r_info)) { 89 case R_TYPE(NONE): 90 break; 91 92#if 1 /* XXX should not occur */ 93 case R_TYPE(PC24): { /* word32 S - P + A */ 94 Elf32_Sword addend; 95 96 /* 97 * Extract addend and sign-extend if needed. 98 */ 99 addend = *where; 100 if (addend & 0x00800000) 101 addend |= 0xff000000; 102 103 def = _rtld_find_symdef(symnum, obj, &defobj, false); 104 if (def == NULL) 105 return -1; 106 tmp = (Elf_Addr)obj->relocbase + def->st_value 107 - (Elf_Addr)where + (addend << 2); 108 if ((tmp & 0xfe000000) != 0xfe000000 && 109 (tmp & 0xfe000000) != 0) { 110 _rtld_error( 111 "%s: R_ARM_PC24 relocation @ %p to %s failed " 112 "(displacement %ld (%#lx) out of range)", 113 obj->path, where, 114 obj->strtab + obj->symtab[symnum].st_name, 115 (long) tmp, (long) tmp); 116 return -1; 117 } 118 tmp >>= 2; 119 *where = (*where & 0xff000000) | (tmp & 0x00ffffff); 120 rdbg(("PC24 %s in %s --> %p @ %p in %s", 121 obj->strtab + obj->symtab[symnum].st_name, 122 obj->path, (void *)*where, where, defobj->path)); 123 break; 124 } 125#endif 126 127 case R_TYPE(ABS32): /* word32 B + S + A */ 128 case R_TYPE(GLOB_DAT): /* word32 B + S */ 129 def = _rtld_find_symdef(symnum, obj, &defobj, false); 130 if (def == NULL) 131 return -1; 132 if (__predict_true(RELOC_ALIGNED_P(where))) { 133 tmp = *where + (Elf_Addr)defobj->relocbase + 134 def->st_value; 135 /* Set the Thumb bit, if needed. */ 136 if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC) 137 tmp |= 1; 138 *where = tmp; 139 } else { 140 tmp = load_ptr(where) + 141 (Elf_Addr)defobj->relocbase + 142 def->st_value; 143 /* Set the Thumb bit, if needed. */ 144 if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC) 145 tmp |= 1; 146 store_ptr(where, tmp); 147 } 148 rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s", 149 obj->strtab + obj->symtab[symnum].st_name, 150 obj->path, (void *)tmp, where, defobj->path)); 151 break; 152 153 case R_TYPE(RELATIVE): /* word32 B + A */ 154 if (__predict_true(RELOC_ALIGNED_P(where))) { 155 tmp = *where + (Elf_Addr)obj->relocbase; 156 *where = tmp; 157 } else { 158 tmp = load_ptr(where) + 159 (Elf_Addr)obj->relocbase; 160 store_ptr(where, tmp); 161 } 162 rdbg(("RELATIVE in %s --> %p", obj->path, 163 (void *)tmp)); 164 break; 165 166 case R_TYPE(COPY): 167 /* 168 * These are deferred until all other relocations have 169 * been done. All we do here is make sure that the 170 * COPY relocation is not in a shared library. They 171 * are allowed only in executable files. 172 */ 173 if (obj->isdynamic) { 174 _rtld_error( 175 "%s: Unexpected R_COPY relocation in shared library", 176 obj->path); 177 return -1; 178 } 179 rdbg(("COPY (avoid in main)")); 180 break; 181 182 case R_TYPE(TLS_DTPOFF32): 183 def = _rtld_find_symdef(symnum, obj, &defobj, false); 184 if (def == NULL) 185 return -1; 186 187 tmp = (Elf_Addr)(def->st_value); 188 if (__predict_true(RELOC_ALIGNED_P(where))) 189 *where = tmp; 190 else 191 store_ptr(where, tmp); 192 193 rdbg(("TLS_DTPOFF32 %s in %s --> %p", 194 obj->strtab + obj->symtab[symnum].st_name, 195 obj->path, (void *)tmp)); 196 197 break; 198 case R_TYPE(TLS_DTPMOD32): 199 def = _rtld_find_symdef(symnum, obj, &defobj, false); 200 if (def == NULL) 201 return -1; 202 203 tmp = (Elf_Addr)(defobj->tlsindex); 204 if (__predict_true(RELOC_ALIGNED_P(where))) 205 *where = tmp; 206 else 207 store_ptr(where, tmp); 208 209 rdbg(("TLS_DTPMOD32 %s in %s --> %p", 210 obj->strtab + obj->symtab[symnum].st_name, 211 obj->path, (void *)tmp)); 212 213 break; 214 215 case R_TYPE(TLS_TPOFF32): 216 def = _rtld_find_symdef(symnum, obj, &defobj, false); 217 if (def == NULL) 218 return -1; 219 220 if (!defobj->tls_done && 221 _rtld_tls_offset_allocate(obj)) 222 return -1; 223 224 tmp = (Elf_Addr)def->st_value + defobj->tlsoffset + 225 sizeof(struct tls_tcb); 226 if (__predict_true(RELOC_ALIGNED_P(where))) 227 *where = tmp; 228 else 229 store_ptr(where, tmp); 230 rdbg(("TLS_TPOFF32 %s in %s --> %p", 231 obj->strtab + obj->symtab[symnum].st_name, 232 obj->path, (void *)tmp)); 233 break; 234 235 default: 236 rdbg(("sym = %lu, type = %lu, offset = %p, " 237 "contents = %p, symbol = %s", 238 symnum, (u_long)ELF_R_TYPE(rel->r_info), 239 (void *)rel->r_offset, (void *)load_ptr(where), 240 obj->strtab + obj->symtab[symnum].st_name)); 241 _rtld_error("%s: Unsupported relocation type %ld " 242 "in non-PLT relocations", 243 obj->path, (u_long) ELF_R_TYPE(rel->r_info)); 244 return -1; 245 } 246 } 247 return 0; 248} 249 250int 251_rtld_relocate_plt_lazy(const Obj_Entry *obj) 252{ 253 const Elf_Rel *rel; 254 255 if (!obj->relocbase) 256 return 0; 257 258 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) { 259 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 260 261 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT)); 262 263 /* Just relocate the GOT slots pointing into the PLT */ 264 *where += (Elf_Addr)obj->relocbase; 265 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where)); 266 } 267 268 return 0; 269} 270 271static int 272_rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel, 273 Elf_Addr *tp) 274{ 275 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 276 Elf_Addr new_value; 277 const Elf_Sym *def; 278 const Obj_Entry *defobj; 279 unsigned long info = rel->r_info; 280 281 assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT)); 282 283 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL); 284 if (__predict_false(def == NULL)) 285 return -1; 286 if (__predict_false(def == &_rtld_sym_zero)) 287 return 0; 288 289 new_value = (Elf_Addr)(defobj->relocbase + def->st_value); 290 /* Set the Thumb bit, if needed. */ 291 if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC) 292 new_value |= 1; 293 rdbg(("bind now/fixup in %s --> old=%p new=%p", 294 defobj->strtab + def->st_name, (void *)*where, (void *)new_value)); 295 if (*where != new_value) 296 *where = new_value; 297 if (tp) 298 *tp = new_value; 299 300 return 0; 301} 302 303caddr_t 304_rtld_bind(const Obj_Entry *obj, Elf_Word reloff) 305{ 306 const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel + reloff); 307 Elf_Addr new_value = 0; /* XXX gcc */ 308 int err; 309 310 _rtld_shared_enter(); 311 err = _rtld_relocate_plt_object(obj, rel, &new_value); 312 if (err) 313 _rtld_die(); 314 _rtld_shared_exit(); 315 316 return (caddr_t)new_value; 317} 318int 319_rtld_relocate_plt_objects(const Obj_Entry *obj) 320{ 321 const Elf_Rel *rel; 322 int err = 0; 323 324 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) { 325 err = _rtld_relocate_plt_object(obj, rel, NULL); 326 if (err) 327 break; 328 } 329 330 return err; 331} 332