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