1 1.38 riastrad /* $NetBSD: mdreloc.c,v 1.38 2025/04/16 17:51:01 riastradh Exp $ */ 2 1.38 riastrad 3 1.38 riastrad /* 4 1.38 riastrad * Copyright 1996 John D. Polstra. 5 1.38 riastrad * Copyright 1996 Matt Thomas <matt (at) 3am-software.com> 6 1.38 riastrad * All rights reserved. 7 1.38 riastrad * 8 1.38 riastrad * Redistribution and use in source and binary forms, with or without 9 1.38 riastrad * modification, are permitted provided that the following conditions 10 1.38 riastrad * are met: 11 1.38 riastrad * 1. Redistributions of source code must retain the above copyright 12 1.38 riastrad * notice, this list of conditions and the following disclaimer. 13 1.38 riastrad * 2. Redistributions in binary form must reproduce the above copyright 14 1.38 riastrad * notice, this list of conditions and the following disclaimer in the 15 1.38 riastrad * documentation and/or other materials provided with the distribution. 16 1.38 riastrad * 3. All advertising materials mentioning features or use of this software 17 1.38 riastrad * must display the following acknowledgement: 18 1.38 riastrad * This product includes software developed by John Polstra. 19 1.38 riastrad * 4. The name of the author may not be used to endorse or promote products 20 1.38 riastrad * derived from this software without specific prior written permission. 21 1.38 riastrad * 22 1.38 riastrad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 1.38 riastrad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 1.38 riastrad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 1.38 riastrad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 1.38 riastrad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 1.38 riastrad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 1.38 riastrad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 1.38 riastrad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 1.38 riastrad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 1.38 riastrad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.38 riastrad */ 33 1.21 skrll 34 1.21 skrll #include <sys/cdefs.h> 35 1.21 skrll #ifndef lint 36 1.38 riastrad __RCSID("$NetBSD: mdreloc.c,v 1.38 2025/04/16 17:51:01 riastradh Exp $"); 37 1.21 skrll #endif /* not lint */ 38 1.10 junyoung 39 1.37 uwe /* 40 1.37 uwe * SuperH ELF relocations. 41 1.37 uwe * 42 1.37 uwe * Reference: 43 1.37 uwe * 44 1.37 uwe * [RM0197] SH-4 generic and C specific application binary interface 45 1.37 uwe * https://www.st.com/resource/en/reference_manual/rm0197-sh4-generic-and-c-specific-application-binary-interface-stmicroelectronics.pdf 46 1.37 uwe */ 47 1.37 uwe 48 1.1 mycroft #include <sys/types.h> 49 1.29 joerg #include <sys/tls.h> 50 1.1 mycroft 51 1.1 mycroft #include "debug.h" 52 1.1 mycroft #include "rtld.h" 53 1.11 mycroft 54 1.11 mycroft void _rtld_bind_start(void); 55 1.17 tsutsui void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr); 56 1.17 tsutsui caddr_t _rtld_bind(const Obj_Entry *, Elf_Word); 57 1.20 skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *, 58 1.20 skrll const Elf_Rela *, Elf_Addr *); 59 1.1 mycroft 60 1.1 mycroft void 61 1.1 mycroft _rtld_setup_pltgot(const Obj_Entry *obj) 62 1.1 mycroft { 63 1.1 mycroft obj->pltgot[1] = (Elf_Addr) obj; 64 1.1 mycroft obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 65 1.1 mycroft } 66 1.2 mycroft 67 1.15 marcus void 68 1.15 marcus _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 69 1.15 marcus { 70 1.15 marcus const Elf_Rela *rela = 0, *relalim; 71 1.15 marcus Elf_Addr relasz = 0; 72 1.15 marcus Elf_Addr *where; 73 1.15 marcus 74 1.15 marcus for (; dynp->d_tag != DT_NULL; dynp++) { 75 1.15 marcus switch (dynp->d_tag) { 76 1.15 marcus case DT_RELA: 77 1.15 marcus rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr); 78 1.15 marcus break; 79 1.15 marcus case DT_RELASZ: 80 1.15 marcus relasz = dynp->d_un.d_val; 81 1.15 marcus break; 82 1.15 marcus } 83 1.15 marcus } 84 1.25 lukem relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz); 85 1.15 marcus for (; rela < relalim; rela++) { 86 1.15 marcus where = (Elf_Addr *)(relocbase + rela->r_offset); 87 1.15 marcus *where = (Elf_Addr)(relocbase + rela->r_addend); 88 1.15 marcus } 89 1.15 marcus } 90 1.15 marcus 91 1.2 mycroft int 92 1.28 joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj) 93 1.2 mycroft { 94 1.3 mycroft const Elf_Rela *rela; 95 1.34 joerg const Elf_Sym *def = NULL; 96 1.34 joerg const Obj_Entry *defobj = NULL; 97 1.34 joerg unsigned long last_symnum = ULONG_MAX; 98 1.2 mycroft 99 1.3 mycroft for (rela = obj->rela; rela < obj->relalim; rela++) { 100 1.3 mycroft Elf_Addr *where; 101 1.3 mycroft Elf_Addr tmp; 102 1.4 mycroft unsigned long symnum; 103 1.3 mycroft 104 1.3 mycroft where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 105 1.33 joerg 106 1.33 joerg switch (ELF_R_TYPE(rela->r_info)) { 107 1.33 joerg case R_TYPE(GOT32): 108 1.33 joerg case R_TYPE(REL32): 109 1.33 joerg case R_TYPE(DIR32): 110 1.33 joerg case R_TYPE(GLOB_DAT): 111 1.33 joerg case R_TYPE(TLS_DTPOFF32): 112 1.33 joerg case R_TYPE(TLS_DTPMOD32): 113 1.33 joerg case R_TYPE(TLS_TPOFF32): 114 1.33 joerg symnum = ELF_R_SYM(rela->r_info); 115 1.33 joerg if (last_symnum != symnum) { 116 1.33 joerg last_symnum = symnum; 117 1.33 joerg def = _rtld_find_symdef(symnum, obj, &defobj, 118 1.33 joerg false); 119 1.33 joerg if (def == NULL) 120 1.33 joerg return -1; 121 1.33 joerg } 122 1.33 joerg break; 123 1.33 joerg default: 124 1.33 joerg break; 125 1.33 joerg } 126 1.3 mycroft 127 1.3 mycroft switch (ELF_R_TYPE(rela->r_info)) { 128 1.3 mycroft case R_TYPE(NONE): 129 1.3 mycroft break; 130 1.2 mycroft 131 1.2 mycroft #if 1 /* XXX should not occur */ 132 1.3 mycroft case R_TYPE(GOT32): 133 1.3 mycroft tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 134 1.3 mycroft rela->r_addend); 135 1.3 mycroft if (*where != tmp) 136 1.3 mycroft *where = tmp; 137 1.12 mycroft rdbg(("GOT32 %s in %s --> %p in %s", 138 1.5 mycroft obj->strtab + obj->symtab[symnum].st_name, 139 1.5 mycroft obj->path, (void *)*where, defobj->path)); 140 1.3 mycroft break; 141 1.3 mycroft 142 1.3 mycroft case R_TYPE(REL32): 143 1.16 marcus tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 144 1.3 mycroft rela->r_addend) - (Elf_Addr)where; 145 1.16 marcus if (*where != tmp) 146 1.16 marcus *where = tmp; 147 1.12 mycroft rdbg(("PC32 %s in %s --> %p in %s", 148 1.5 mycroft obj->strtab + obj->symtab[symnum].st_name, 149 1.5 mycroft obj->path, (void *)*where, defobj->path)); 150 1.3 mycroft break; 151 1.2 mycroft #endif 152 1.2 mycroft 153 1.3 mycroft case R_TYPE(DIR32): 154 1.16 marcus tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 155 1.3 mycroft rela->r_addend); 156 1.16 marcus if (*where != tmp) 157 1.16 marcus *where = tmp; 158 1.12 mycroft rdbg(("32 %s in %s --> %p in %s", 159 1.5 mycroft obj->strtab + obj->symtab[symnum].st_name, 160 1.5 mycroft obj->path, (void *)*where, defobj->path)); 161 1.3 mycroft break; 162 1.3 mycroft 163 1.3 mycroft case R_TYPE(GLOB_DAT): 164 1.3 mycroft tmp = (Elf_Addr)(defobj->relocbase + def->st_value) + 165 1.3 mycroft rela->r_addend; 166 1.3 mycroft if (*where != tmp) 167 1.3 mycroft *where = tmp; 168 1.12 mycroft rdbg(("GLOB_DAT %s in %s --> %p in %s", 169 1.5 mycroft obj->strtab + obj->symtab[symnum].st_name, 170 1.5 mycroft obj->path, (void *)*where, defobj->path)); 171 1.3 mycroft break; 172 1.3 mycroft 173 1.3 mycroft case R_TYPE(RELATIVE): 174 1.3 mycroft if (rela->r_addend) 175 1.3 mycroft *where = (Elf_Addr)obj->relocbase + rela->r_addend; 176 1.3 mycroft else 177 1.3 mycroft *where += (Elf_Addr)obj->relocbase; 178 1.12 mycroft rdbg(("RELATIVE in %s --> %p", obj->path, 179 1.3 mycroft (void *)*where)); 180 1.3 mycroft break; 181 1.3 mycroft 182 1.3 mycroft case R_TYPE(COPY): 183 1.3 mycroft /* 184 1.3 mycroft * These are deferred until all other relocations have 185 1.3 mycroft * been done. All we do here is make sure that the 186 1.3 mycroft * COPY relocation is not in a shared library. They 187 1.3 mycroft * are allowed only in executable files. 188 1.3 mycroft */ 189 1.8 mycroft if (obj->isdynamic) { 190 1.3 mycroft _rtld_error( 191 1.2 mycroft "%s: Unexpected R_COPY relocation in shared library", 192 1.3 mycroft obj->path); 193 1.3 mycroft return -1; 194 1.3 mycroft } 195 1.12 mycroft rdbg(("COPY (avoid in main)")); 196 1.3 mycroft break; 197 1.3 mycroft 198 1.29 joerg case R_TYPE(TLS_DTPOFF32): 199 1.29 joerg *where = (Elf_Addr)(def->st_value); 200 1.29 joerg 201 1.29 joerg rdbg(("TLS_DTPOFF32 %s in %s --> %p", 202 1.29 joerg obj->strtab + obj->symtab[symnum].st_name, 203 1.29 joerg obj->path, (void *)*where)); 204 1.29 joerg 205 1.29 joerg break; 206 1.29 joerg case R_TYPE(TLS_DTPMOD32): 207 1.29 joerg *where = (Elf_Addr)(defobj->tlsindex); 208 1.29 joerg 209 1.29 joerg rdbg(("TLS_DTPMOD32 %s in %s --> %p", 210 1.29 joerg obj->strtab + obj->symtab[symnum].st_name, 211 1.29 joerg obj->path, (void *)*where)); 212 1.29 joerg 213 1.29 joerg break; 214 1.29 joerg 215 1.29 joerg case R_TYPE(TLS_TPOFF32): 216 1.36 joerg if (!defobj->tls_static && 217 1.36 joerg _rtld_tls_offset_allocate(__UNCONST(defobj))) 218 1.29 joerg return -1; 219 1.29 joerg 220 1.29 joerg *where = (Elf_Addr)def->st_value + 221 1.29 joerg rela->r_addend + defobj->tlsoffset + 222 1.29 joerg sizeof(struct tls_tcb); 223 1.29 joerg 224 1.29 joerg rdbg(("TLS_TPOFF32 %s in %s --> %p", 225 1.29 joerg obj->strtab + obj->symtab[symnum].st_name, 226 1.29 joerg obj->path, (void *)*where)); 227 1.29 joerg break; 228 1.29 joerg 229 1.3 mycroft default: 230 1.12 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, " 231 1.3 mycroft "addend = %p, contents = %p, symbol = %s", 232 1.33 joerg (u_long)ELF_R_SYM(rela->r_info), 233 1.33 joerg (u_long)ELF_R_TYPE(rela->r_info), 234 1.3 mycroft (void *)rela->r_offset, (void *)rela->r_addend, 235 1.3 mycroft (void *)*where, 236 1.4 mycroft obj->strtab + obj->symtab[symnum].st_name)); 237 1.3 mycroft _rtld_error("%s: Unsupported relocation type %ld " 238 1.26 jmmv "in non-PLT relocations", 239 1.3 mycroft obj->path, (u_long) ELF_R_TYPE(rela->r_info)); 240 1.2 mycroft return -1; 241 1.2 mycroft } 242 1.2 mycroft } 243 1.2 mycroft return 0; 244 1.2 mycroft } 245 1.6 mycroft 246 1.6 mycroft int 247 1.35 joerg _rtld_relocate_plt_lazy(Obj_Entry *obj) 248 1.6 mycroft { 249 1.6 mycroft const Elf_Rela *rela; 250 1.6 mycroft 251 1.14 mycroft if (!obj->relocbase) 252 1.6 mycroft return 0; 253 1.6 mycroft 254 1.6 mycroft for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) { 255 1.6 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 256 1.6 mycroft 257 1.6 mycroft assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)); 258 1.6 mycroft 259 1.6 mycroft /* Just relocate the GOT slots pointing into the PLT */ 260 1.6 mycroft *where += (Elf_Addr)obj->relocbase; 261 1.12 mycroft rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where)); 262 1.6 mycroft } 263 1.6 mycroft 264 1.6 mycroft return 0; 265 1.15 marcus } 266 1.15 marcus 267 1.15 marcus caddr_t 268 1.19 skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff) 269 1.15 marcus { 270 1.25 lukem const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff); 271 1.20 skrll Elf_Addr new_value; 272 1.20 skrll int err; 273 1.20 skrll 274 1.22 mrg new_value = 0; /* XXX gcc */ 275 1.22 mrg 276 1.30 joerg _rtld_shared_enter(); 277 1.20 skrll err = _rtld_relocate_plt_object(obj, rela, &new_value); 278 1.27 christos if (err) 279 1.20 skrll _rtld_die(); 280 1.30 joerg _rtld_shared_exit(); 281 1.20 skrll 282 1.20 skrll return (caddr_t)new_value; 283 1.20 skrll } 284 1.20 skrll 285 1.20 skrll int 286 1.20 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj) 287 1.20 skrll { 288 1.20 skrll const Elf_Rela *rela = obj->pltrela; 289 1.20 skrll 290 1.20 skrll for (; rela < obj->pltrelalim; rela++) 291 1.20 skrll if (_rtld_relocate_plt_object(obj, rela, NULL) < 0) 292 1.20 skrll return -1; 293 1.20 skrll 294 1.20 skrll return 0; 295 1.20 skrll } 296 1.20 skrll 297 1.20 skrll static inline int 298 1.20 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp) 299 1.20 skrll { 300 1.15 marcus Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 301 1.15 marcus Elf_Addr new_value; 302 1.15 marcus const Elf_Sym *def; 303 1.15 marcus const Obj_Entry *defobj; 304 1.27 christos unsigned long info = rela->r_info; 305 1.15 marcus 306 1.27 christos assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT)); 307 1.15 marcus 308 1.27 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL); 309 1.27 christos if (__predict_false(def == NULL)) 310 1.20 skrll return -1; 311 1.27 christos if (__predict_false(def == &_rtld_sym_zero)) 312 1.27 christos return 0; 313 1.15 marcus 314 1.31 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { 315 1.31 joerg if (tp == NULL) 316 1.31 joerg return 0; 317 1.31 joerg new_value = _rtld_resolve_ifunc(defobj, def); 318 1.31 joerg } else { 319 1.31 joerg new_value = (Elf_Addr)(defobj->relocbase + def->st_value); 320 1.31 joerg } 321 1.15 marcus rdbg(("bind now/fixup in %s --> old=%p new=%p", 322 1.15 marcus defobj->strtab + def->st_name, (void *)*where, (void *)new_value)); 323 1.15 marcus if (*where != new_value) 324 1.15 marcus *where = new_value; 325 1.15 marcus 326 1.20 skrll if (tp) 327 1.20 skrll *tp = new_value; 328 1.20 skrll 329 1.20 skrll return 0; 330 1.6 mycroft } 331