1 /* $NetBSD: mdreloc.c,v 1.33 2017/08/10 19:03:26 joerg Exp $ */ 2 3 #include <sys/cdefs.h> 4 #ifndef lint 5 __RCSID("$NetBSD: mdreloc.c,v 1.33 2017/08/10 19:03:26 joerg Exp $"); 6 #endif /* not lint */ 7 8 #include <sys/types.h> 9 10 #include "debug.h" 11 #include "rtld.h" 12 13 void _rtld_bind_start(void); 14 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr); 15 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word); 16 static inline int _rtld_relocate_plt_object(const Obj_Entry *, 17 const Elf_Rela *, Elf_Addr *); 18 19 20 void 21 _rtld_setup_pltgot(const Obj_Entry *obj) 22 { 23 obj->pltgot[1] = (Elf_Addr) obj; 24 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 25 } 26 27 void 28 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 29 { 30 const Elf_Rela *rela = 0, *relalim; 31 Elf_Addr relasz = 0; 32 Elf_Addr *where; 33 34 for (; dynp->d_tag != DT_NULL; dynp++) { 35 switch (dynp->d_tag) { 36 case DT_RELA: 37 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr); 38 break; 39 case DT_RELASZ: 40 relasz = dynp->d_un.d_val; 41 break; 42 } 43 } 44 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz); 45 for (; rela < relalim; rela++) { 46 where = (Elf_Addr *)(relocbase + rela->r_offset); 47 *where += (Elf_Addr)relocbase; 48 } 49 } 50 51 int 52 _rtld_relocate_nonplt_objects(Obj_Entry *obj) 53 { 54 const Elf_Rela *rela; 55 const Elf_Sym *def = NULL; 56 const Obj_Entry *defobj = NULL; 57 unsigned long last_symnum = ULONG_MAX; 58 59 for (rela = obj->rela; rela < obj->relalim; rela++) { 60 Elf_Addr *where; 61 Elf_Addr tmp; 62 unsigned long symnum; 63 64 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 65 66 switch (ELF_R_TYPE(rela->r_info)) { 67 case R_TYPE(PC32): 68 case R_TYPE(GOT32): 69 case R_TYPE(32): 70 case R_TYPE(GLOB_DAT): 71 case R_TYPE(TLS_DTPMOD32): 72 case R_TYPE(TLS_DTPREL32): 73 case R_TYPE(TLS_TPREL32): 74 symnum = ELF_R_SYM(rela->r_info); 75 if (last_symnum != symnum) { 76 last_symnum = symnum; 77 def = _rtld_find_symdef(symnum, obj, &defobj, 78 false); 79 if (def == NULL) 80 return -1; 81 } 82 break; 83 default: 84 break; 85 } 86 87 switch (ELF_R_TYPE(rela->r_info)) { 88 case R_TYPE(NONE): 89 break; 90 91 #if 1 /* XXX should not occur */ 92 case R_TYPE(PC32): 93 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 94 rela->r_addend) - (Elf_Addr)where; 95 if (*where != tmp) 96 *where = tmp; 97 rdbg(("PC32 %s in %s --> %p in %s", 98 obj->strtab + obj->symtab[symnum].st_name, 99 obj->path, (void *)*where, defobj->path)); 100 break; 101 102 case R_TYPE(GOT32): 103 #endif 104 case R_TYPE(32): 105 case R_TYPE(GLOB_DAT): 106 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 107 rela->r_addend); 108 if (*where != tmp) 109 *where = tmp; 110 rdbg(("32/GLOB_DAT %s in %s --> %p in %s", 111 obj->strtab + obj->symtab[symnum].st_name, 112 obj->path, (void *)*where, defobj->path)); 113 break; 114 115 case R_TYPE(RELATIVE): 116 *where += (Elf_Addr)obj->relocbase; 117 rdbg(("RELATIVE in %s --> %p", obj->path, 118 (void *)*where)); 119 break; 120 121 case R_TYPE(COPY): 122 /* 123 * These are deferred until all other relocations have 124 * been done. All we do here is make sure that the 125 * COPY relocation is not in a shared library. They 126 * are allowed only in executable files. 127 */ 128 if (obj->isdynamic) { 129 _rtld_error( 130 "%s: Unexpected R_COPY relocation in shared library", 131 obj->path); 132 return -1; 133 } 134 rdbg(("COPY (avoid in main)")); 135 break; 136 137 case R_TYPE(TLS_DTPMOD32): 138 *where = (Elf_Addr)defobj->tlsindex; 139 rdbg(("DTPMOD32 %s in %s --> %p in %s", 140 obj->strtab + obj->symtab[symnum].st_name, 141 obj->path, (void *)*where, defobj->path)); 142 break; 143 144 case R_TYPE(TLS_DTPREL32): 145 if (!defobj->tls_done && _rtld_tls_offset_allocate(obj)) 146 return -1; 147 148 *where = (Elf_Addr)(def->st_value + rela->r_addend 149 - TLS_DTV_OFFSET); 150 rdbg(("DTPREL32 %s in %s --> %p in %s", 151 obj->strtab + obj->symtab[symnum].st_name, 152 obj->path, (void *)*where, defobj->path)); 153 break; 154 155 case R_TYPE(TLS_TPREL32): 156 if (!defobj->tls_done && _rtld_tls_offset_allocate(obj)) 157 return -1; 158 159 *where = (Elf_Addr)(def->st_value + rela->r_addend 160 + defobj->tlsoffset - TLS_TP_OFFSET); 161 rdbg(("TPREL32 %s in %s --> %p in %s", 162 obj->strtab + obj->symtab[symnum].st_name, 163 obj->path, (void *)*where, defobj->path)); 164 break; 165 166 default: 167 rdbg(("sym = %lu, type = %lu, offset = %p, " 168 "addend = %p, contents = %p, symbol = %s", 169 (u_long)ELF_R_SYM(rela->r_info), 170 (u_long)ELF_R_TYPE(rela->r_info), 171 (void *)rela->r_offset, (void *)rela->r_addend, 172 (void *)*where, 173 obj->strtab + obj->symtab[symnum].st_name)); 174 _rtld_error("%s: Unsupported relocation type %ld " 175 "in non-PLT relocations", 176 obj->path, (u_long) ELF_R_TYPE(rela->r_info)); 177 return -1; 178 } 179 } 180 return 0; 181 } 182 183 int 184 _rtld_relocate_plt_lazy(Obj_Entry *obj) 185 { 186 const Elf_Rela *rela; 187 188 if (!obj->relocbase) 189 return 0; 190 191 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) { 192 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 193 194 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)); 195 196 /* Just relocate the GOT slots pointing into the PLT */ 197 *where += (Elf_Addr)obj->relocbase; 198 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where)); 199 } 200 201 return 0; 202 } 203 204 static inline int 205 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, 206 Elf_Addr *tp) 207 { 208 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 209 Elf_Addr new_value; 210 const Elf_Sym *def; 211 const Obj_Entry *defobj; 212 unsigned long info = rela->r_info; 213 214 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT)); 215 216 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL); 217 if (__predict_false(def == NULL)) 218 return -1; 219 if (__predict_false(def == &_rtld_sym_zero)) 220 return 0; 221 222 assert(rela->r_addend == 0); 223 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { 224 if (tp == NULL) 225 return 0; 226 new_value = _rtld_resolve_ifunc(defobj, def); 227 } else { 228 new_value = (Elf_Addr)(defobj->relocbase + def->st_value + 229 rela->r_addend); 230 } 231 rdbg(("bind now/fixup in %s --> old=%p new=%p", 232 defobj->strtab + def->st_name, (void *)*where, (void *)new_value)); 233 if (*where != new_value) 234 *where = new_value; 235 236 if (tp) 237 *tp = new_value - rela->r_addend; 238 239 return 0; 240 } 241 242 caddr_t 243 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff) 244 { 245 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff); 246 Elf_Addr result; 247 int err; 248 249 result = 0; /* XXX gcc */ 250 251 _rtld_shared_enter(); 252 err = _rtld_relocate_plt_object(obj, rela, &result); 253 if (err) 254 _rtld_die(); 255 _rtld_shared_exit(); 256 257 return (caddr_t)result; 258 } 259 260 int 261 _rtld_relocate_plt_objects(const Obj_Entry *obj) 262 { 263 const Elf_Rela *rela; 264 265 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) 266 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0) 267 return -1; 268 269 return 0; 270 } 271