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