1 /* $NetBSD: mdreloc.c,v 1.21 2005/08/20 19:01:17 skrll Exp $ */ 2 3 #include <sys/cdefs.h> 4 #ifndef lint 5 __RCSID("$NetBSD: mdreloc.c,v 1.21 2005/08/20 19:01:17 skrll Exp $"); 6 #endif /* not lint */ 7 8 #include <sys/types.h> 9 #include <sys/stat.h> 10 11 #include "debug.h" 12 #include "rtld.h" 13 14 void _rtld_bind_start(void); 15 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr); 16 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word); 17 18 void 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 25 void 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 *)((caddr_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 int 50 _rtld_relocate_nonplt_objects(const Obj_Entry *obj) 51 { 52 const Elf_Rel *rel; 53 #define COMBRELOC 54 #ifdef COMBRELOC 55 unsigned long lastsym = -1; 56 #endif 57 Elf_Addr target = 0; 58 59 for (rel = obj->rel; rel < obj->rellim; rel++) { 60 Elf_Addr *where; 61 const Elf_Sym *def; 62 const Obj_Entry *defobj; 63 Elf_Addr tmp; 64 unsigned long symnum; 65 66 where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 67 symnum = ELF_R_SYM(rel->r_info); 68 69 switch (ELF_R_TYPE(rel->r_info)) { 70 case R_TYPE(NONE): 71 break; 72 73 #if 1 /* XXX should not occur */ 74 case R_TYPE(PC32): 75 #ifdef COMBRELOC 76 if (symnum != lastsym) { 77 #endif 78 def = _rtld_find_symdef(symnum, obj, &defobj, 79 false); 80 if (def == NULL) 81 return -1; 82 target = (Elf_Addr)(defobj->relocbase + 83 def->st_value); 84 #ifdef COMBRELOC 85 lastsym = symnum; 86 } 87 #endif 88 89 *where += target - (Elf_Addr)where; 90 rdbg(("PC32 %s in %s --> %p in %s", 91 obj->strtab + obj->symtab[symnum].st_name, 92 obj->path, (void *)*where, defobj->path)); 93 break; 94 95 case R_TYPE(GOT32): 96 #endif 97 case R_TYPE(32): 98 case R_TYPE(GLOB_DAT): 99 #ifdef COMBRELOC 100 if (symnum != lastsym) { 101 #endif 102 def = _rtld_find_symdef(symnum, obj, &defobj, 103 false); 104 if (def == NULL) 105 return -1; 106 target = (Elf_Addr)(defobj->relocbase + 107 def->st_value); 108 #ifdef COMBRELOC 109 lastsym = symnum; 110 } 111 #endif 112 113 tmp = target + *where; 114 if (*where != tmp) 115 *where = tmp; 116 rdbg(("32/GLOB_DAT %s in %s --> %p in %s", 117 obj->strtab + obj->symtab[symnum].st_name, 118 obj->path, (void *)*where, defobj->path)); 119 break; 120 121 case R_TYPE(RELATIVE): 122 *where += (Elf_Addr)obj->relocbase; 123 rdbg(("RELATIVE in %s --> %p", obj->path, 124 (void *)*where)); 125 break; 126 127 case R_TYPE(COPY): 128 /* 129 * These are deferred until all other relocations have 130 * been done. All we do here is make sure that the 131 * COPY relocation is not in a shared library. They 132 * are allowed only in executable files. 133 */ 134 if (obj->isdynamic) { 135 _rtld_error( 136 "%s: Unexpected R_COPY relocation in shared library", 137 obj->path); 138 return -1; 139 } 140 rdbg(("COPY (avoid in main)")); 141 break; 142 143 default: 144 rdbg(("sym = %lu, type = %lu, offset = %p, " 145 "contents = %p, symbol = %s", 146 symnum, (u_long)ELF_R_TYPE(rel->r_info), 147 (void *)rel->r_offset, (void *)*where, 148 obj->strtab + obj->symtab[symnum].st_name)); 149 _rtld_error("%s: Unsupported relocation type %ld " 150 "in non-PLT relocations\n", 151 obj->path, (u_long) ELF_R_TYPE(rel->r_info)); 152 return -1; 153 } 154 } 155 return 0; 156 } 157 158 int 159 _rtld_relocate_plt_lazy(const Obj_Entry *obj) 160 { 161 const Elf_Rel *rel; 162 163 if (!obj->relocbase) 164 return 0; 165 166 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) { 167 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 168 169 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT)); 170 171 /* Just relocate the GOT slots pointing into the PLT */ 172 *where += (Elf_Addr)obj->relocbase; 173 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where)); 174 } 175 176 return 0; 177 } 178 179 caddr_t 180 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff) 181 { 182 const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff); 183 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 184 Elf_Addr new_value; 185 const Elf_Sym *def; 186 const Obj_Entry *defobj; 187 188 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT)); 189 190 def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true); 191 if (def == NULL) 192 _rtld_die(); 193 194 new_value = (Elf_Addr)(defobj->relocbase + def->st_value); 195 rdbg(("bind now/fixup in %s --> old=%p new=%p", 196 defobj->strtab + def->st_name, (void *)*where, (void *)new_value)); 197 if (*where != new_value) 198 *where = new_value; 199 200 return (caddr_t)new_value; 201 } 202 203 int 204 _rtld_relocate_plt_objects(const Obj_Entry *obj) 205 { 206 const Elf_Rel *rel; 207 208 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) { 209 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset); 210 Elf_Addr target; 211 const Elf_Sym *def; 212 const Obj_Entry *defobj; 213 214 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT)); 215 216 def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, 217 true); 218 if (def == NULL) 219 return -1; 220 target = (Elf_Addr)(defobj->relocbase + def->st_value); 221 rdbg(("bind now/fixup in %s --> old=%p new=%p", 222 defobj->strtab + def->st_name, (void *)*where, 223 (void *)target)); 224 if (*where != target) 225 *where = target; 226 } 227 return 0; 228 } 229