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