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