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