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