reloc.c revision 38816
1/*- 2 * Copyright 1996-1998 John D. Polstra. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * $Id: rtld.c,v 1.3 1998/05/01 08:39:27 dfr Exp $ 26 */ 27 28/* 29 * Dynamic linker for ELF. 30 * 31 * John Polstra <jdp@polstra.com>. 32 */ 33 34#include <sys/param.h> 35#include <sys/mman.h> 36 37#include <dlfcn.h> 38#include <err.h> 39#include <errno.h> 40#include <fcntl.h> 41#include <stdarg.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46 47#include "debug.h" 48#include "rtld.h" 49 50/* 51 * Debugging support. 52 */ 53 54#define assert(cond) ((cond) ? (void) 0 :\ 55 (msg("oops: " __XSTRING(__LINE__) "\n"), abort())) 56#define msg(s) (write(1, s, strlen(s))) 57#define trace() msg("trace: " __XSTRING(__LINE__) "\n"); 58 59/* 60 * Process the special R_386_COPY relocations in the main program. These 61 * copy data from a shared object into a region in the main program's BSS 62 * segment. 63 * 64 * Returns 0 on success, -1 on failure. 65 */ 66int 67do_copy_relocations(Obj_Entry *dstobj) 68{ 69 const Elf_Rel *rellim; 70 const Elf_Rel *rel; 71 72 assert(dstobj->mainprog); /* COPY relocations are invalid elsewhere */ 73 74 rellim = (const Elf_Rel *) ((caddr_t) dstobj->rel + dstobj->relsize); 75 for (rel = dstobj->rel; rel < rellim; rel++) { 76 if (ELF_R_TYPE(rel->r_info) == R_386_COPY) { 77 void *dstaddr; 78 const Elf_Sym *dstsym; 79 const char *name; 80 unsigned long hash; 81 size_t size; 82 const void *srcaddr; 83 const Elf_Sym *srcsym; 84 Obj_Entry *srcobj; 85 86 dstaddr = (void *) (dstobj->relocbase + rel->r_offset); 87 dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info); 88 name = dstobj->strtab + dstsym->st_name; 89 hash = elf_hash(name); 90 size = dstsym->st_size; 91 92 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next) 93 if ((srcsym = symlook_obj(name, hash, srcobj, false)) != NULL) 94 break; 95 96 if (srcobj == NULL) { 97 _rtld_error("Undefined symbol \"%s\" referenced from COPY" 98 " relocation in %s", name, dstobj->path); 99 return -1; 100 } 101 102 srcaddr = (const void *) (srcobj->relocbase + srcsym->st_value); 103 memcpy(dstaddr, srcaddr, size); 104 } 105 } 106 107 return 0; 108} 109 110/* Process the non-PLT relocations. */ 111int 112reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld) 113{ 114 const Elf_Rel *rellim; 115 const Elf_Rel *rel; 116 117 rellim = (const Elf_Rel *) ((caddr_t) obj->rel + obj->relsize); 118 for (rel = obj->rel; rel < rellim; rel++) { 119 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rel->r_offset); 120 121 switch (ELF_R_TYPE(rel->r_info)) { 122 123 case R_386_NONE: 124 break; 125 126 case R_386_32: 127 { 128 const Elf_Sym *def; 129 const Obj_Entry *defobj; 130 131 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 132 false); 133 if (def == NULL) 134 return -1; 135 136 *where += (Elf_Addr) (defobj->relocbase + def->st_value); 137 } 138 break; 139 140 case R_386_PC32: 141 /* 142 * I don't think the dynamic linker should ever see this 143 * type of relocation. But the binutils-2.6 tools sometimes 144 * generate it. 145 */ 146 { 147 const Elf_Sym *def; 148 const Obj_Entry *defobj; 149 150 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 151 false); 152 if (def == NULL) 153 return -1; 154 155 *where += 156 (Elf_Addr) (defobj->relocbase + def->st_value) - 157 (Elf_Addr) where; 158 } 159 break; 160 161 case R_386_COPY: 162 /* 163 * These are deferred until all other relocations have 164 * been done. All we do here is make sure that the COPY 165 * relocation is not in a shared library. They are allowed 166 * only in executable files. 167 */ 168 if (!obj->mainprog) { 169 _rtld_error("%s: Unexpected R_386_COPY relocation" 170 " in shared library", obj->path); 171 return -1; 172 } 173 break; 174 175 case R_386_GLOB_DAT: 176 { 177 const Elf_Sym *def; 178 const Obj_Entry *defobj; 179 180 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 181 false); 182 if (def == NULL) 183 return -1; 184 185 *where = (Elf_Addr) (defobj->relocbase + def->st_value); 186 } 187 break; 188 189 case R_386_RELATIVE: 190 *where += (Elf_Addr) obj->relocbase; 191 break; 192 193 default: 194 _rtld_error("%s: Unsupported relocation type %d" 195 " in non-PLT relocations\n", obj->path, 196 ELF_R_TYPE(rel->r_info)); 197 return -1; 198 } 199 } 200 return 0; 201} 202 203/* Process the PLT relocations. */ 204int 205reloc_plt(Obj_Entry *obj, bool bind_now) 206{ 207 const Elf_Rel *rellim; 208 const Elf_Rel *rel; 209 210 /* Process the PLT relocations. */ 211 rellim = (const Elf_Rel *) ((caddr_t) obj->pltrel + obj->pltrelsize); 212 if (bind_now) { 213 /* Fully resolve procedure addresses now */ 214 for (rel = obj->pltrel; rel < rellim; rel++) { 215 Elf_Addr *where = (Elf_Addr *) 216 (obj->relocbase + rel->r_offset); 217 const Elf_Sym *def; 218 const Obj_Entry *defobj; 219 220 assert(ELF_R_TYPE(rel->r_info) == R_386_JMP_SLOT); 221 222 def = find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true); 223 if (def == NULL) 224 return -1; 225 226 *where = (Elf_Addr) (defobj->relocbase + def->st_value); 227 } 228 } else { /* Just relocate the GOT slots pointing into the PLT */ 229 for (rel = obj->pltrel; rel < rellim; rel++) { 230 Elf_Addr *where = (Elf_Addr *) 231 (obj->relocbase + rel->r_offset); 232 *where += (Elf_Addr) obj->relocbase; 233 } 234 } 235 return 0; 236} 237