mdreloc.c revision 1.33 1 1.33 joerg /* $NetBSD: mdreloc.c,v 1.33 2017/08/10 19:03:26 joerg Exp $ */
2 1.19 skrll
3 1.19 skrll #include <sys/cdefs.h>
4 1.19 skrll #ifndef lint
5 1.33 joerg __RCSID("$NetBSD: mdreloc.c,v 1.33 2017/08/10 19:03:26 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 if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
146 1.29 joerg return -1;
147 1.29 joerg
148 1.29 joerg *where = (Elf_Addr)(def->st_value + rela->r_addend
149 1.29 joerg - TLS_DTV_OFFSET);
150 1.29 joerg rdbg(("DTPREL32 %s in %s --> %p in %s",
151 1.29 joerg obj->strtab + obj->symtab[symnum].st_name,
152 1.29 joerg obj->path, (void *)*where, defobj->path));
153 1.29 joerg break;
154 1.29 joerg
155 1.29 joerg case R_TYPE(TLS_TPREL32):
156 1.29 joerg if (!defobj->tls_done && _rtld_tls_offset_allocate(obj))
157 1.29 joerg return -1;
158 1.29 joerg
159 1.29 joerg *where = (Elf_Addr)(def->st_value + rela->r_addend
160 1.29 joerg + defobj->tlsoffset - TLS_TP_OFFSET);
161 1.29 joerg rdbg(("TPREL32 %s in %s --> %p in %s",
162 1.29 joerg obj->strtab + obj->symtab[symnum].st_name,
163 1.29 joerg obj->path, (void *)*where, defobj->path));
164 1.29 joerg break;
165 1.29 joerg
166 1.3 mycroft default:
167 1.13 mycroft rdbg(("sym = %lu, type = %lu, offset = %p, "
168 1.3 mycroft "addend = %p, contents = %p, symbol = %s",
169 1.32 joerg (u_long)ELF_R_SYM(rela->r_info),
170 1.32 joerg (u_long)ELF_R_TYPE(rela->r_info),
171 1.3 mycroft (void *)rela->r_offset, (void *)rela->r_addend,
172 1.3 mycroft (void *)*where,
173 1.4 mycroft obj->strtab + obj->symtab[symnum].st_name));
174 1.3 mycroft _rtld_error("%s: Unsupported relocation type %ld "
175 1.24 jmmv "in non-PLT relocations",
176 1.3 mycroft obj->path, (u_long) ELF_R_TYPE(rela->r_info));
177 1.2 mycroft return -1;
178 1.2 mycroft }
179 1.2 mycroft }
180 1.2 mycroft return 0;
181 1.2 mycroft }
182 1.6 mycroft
183 1.6 mycroft int
184 1.33 joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
185 1.6 mycroft {
186 1.6 mycroft const Elf_Rela *rela;
187 1.6 mycroft
188 1.16 mycroft if (!obj->relocbase)
189 1.6 mycroft return 0;
190 1.6 mycroft
191 1.6 mycroft for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
192 1.6 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
193 1.6 mycroft
194 1.6 mycroft assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
195 1.6 mycroft
196 1.6 mycroft /* Just relocate the GOT slots pointing into the PLT */
197 1.6 mycroft *where += (Elf_Addr)obj->relocbase;
198 1.13 mycroft rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
199 1.6 mycroft }
200 1.6 mycroft
201 1.6 mycroft return 0;
202 1.6 mycroft }
203 1.6 mycroft
204 1.18 skrll static inline int
205 1.26 skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
206 1.26 skrll Elf_Addr *tp)
207 1.6 mycroft {
208 1.6 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
209 1.6 mycroft Elf_Addr new_value;
210 1.6 mycroft const Elf_Sym *def;
211 1.6 mycroft const Obj_Entry *defobj;
212 1.25 christos unsigned long info = rela->r_info;
213 1.6 mycroft
214 1.25 christos assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
215 1.6 mycroft
216 1.25 christos def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
217 1.25 christos if (__predict_false(def == NULL))
218 1.18 skrll return -1;
219 1.25 christos if (__predict_false(def == &_rtld_sym_zero))
220 1.25 christos return 0;
221 1.6 mycroft
222 1.18 skrll assert(rela->r_addend == 0);
223 1.30 joerg if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
224 1.30 joerg if (tp == NULL)
225 1.30 joerg return 0;
226 1.30 joerg new_value = _rtld_resolve_ifunc(defobj, def);
227 1.30 joerg } else {
228 1.30 joerg new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
229 1.30 joerg rela->r_addend);
230 1.30 joerg }
231 1.13 mycroft rdbg(("bind now/fixup in %s --> old=%p new=%p",
232 1.6 mycroft defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
233 1.6 mycroft if (*where != new_value)
234 1.6 mycroft *where = new_value;
235 1.6 mycroft
236 1.18 skrll if (tp)
237 1.18 skrll *tp = new_value - rela->r_addend;
238 1.18 skrll
239 1.18 skrll return 0;
240 1.18 skrll }
241 1.18 skrll
242 1.18 skrll caddr_t
243 1.18 skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
244 1.18 skrll {
245 1.23 lukem const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
246 1.18 skrll Elf_Addr result;
247 1.18 skrll int err;
248 1.18 skrll
249 1.20 mrg result = 0; /* XXX gcc */
250 1.20 mrg
251 1.28 joerg _rtld_shared_enter();
252 1.18 skrll err = _rtld_relocate_plt_object(obj, rela, &result);
253 1.25 christos if (err)
254 1.18 skrll _rtld_die();
255 1.28 joerg _rtld_shared_exit();
256 1.18 skrll
257 1.18 skrll return (caddr_t)result;
258 1.18 skrll }
259 1.18 skrll
260 1.18 skrll int
261 1.18 skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
262 1.18 skrll {
263 1.18 skrll const Elf_Rela *rela;
264 1.18 skrll
265 1.18 skrll for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
266 1.18 skrll if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
267 1.18 skrll return -1;
268 1.18 skrll
269 1.18 skrll return 0;
270 1.6 mycroft }
271