Move primary cache code to libminixfs.
[minix.git] / libexec / ld.elf_so / arch / sh3 / mdreloc.c
blob6ceb4ca0a24b6848ba6eacf800ad2091de81ec40
1 /* $NetBSD: mdreloc.c,v 1.28 2010/08/06 16:33:18 joerg Exp $ */
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.28 2010/08/06 16:33:18 joerg Exp $");
6 #endif /* not lint */
8 #include <sys/cdefs.h>
9 #ifndef lint
10 __RCSID("$NetBSD: mdreloc.c,v 1.28 2010/08/06 16:33:18 joerg Exp $");
11 #endif /* not lint */
13 #include <sys/types.h>
15 #include "debug.h"
16 #include "rtld.h"
18 void _rtld_bind_start(void);
19 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
20 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
21 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
22 const Elf_Rela *, Elf_Addr *);
24 void
25 _rtld_setup_pltgot(const Obj_Entry *obj)
27 obj->pltgot[1] = (Elf_Addr) obj;
28 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
31 void
32 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
34 const Elf_Rela *rela = 0, *relalim;
35 Elf_Addr relasz = 0;
36 Elf_Addr *where;
38 for (; dynp->d_tag != DT_NULL; dynp++) {
39 switch (dynp->d_tag) {
40 case DT_RELA:
41 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
42 break;
43 case DT_RELASZ:
44 relasz = dynp->d_un.d_val;
45 break;
48 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
49 for (; rela < relalim; rela++) {
50 where = (Elf_Addr *)(relocbase + rela->r_offset);
51 *where = (Elf_Addr)(relocbase + rela->r_addend);
55 int
56 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
58 const Elf_Rela *rela;
60 for (rela = obj->rela; rela < obj->relalim; rela++) {
61 Elf_Addr *where;
62 const Elf_Sym *def;
63 const Obj_Entry *defobj;
64 Elf_Addr tmp;
65 unsigned long symnum;
67 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
68 symnum = ELF_R_SYM(rela->r_info);
70 switch (ELF_R_TYPE(rela->r_info)) {
71 case R_TYPE(NONE):
72 break;
74 #if 1 /* XXX should not occur */
75 case R_TYPE(GOT32):
76 def = _rtld_find_symdef(symnum, obj, &defobj, false);
77 if (def == NULL)
78 return -1;
80 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
81 rela->r_addend);
82 if (*where != tmp)
83 *where = tmp;
84 rdbg(("GOT32 %s in %s --> %p in %s",
85 obj->strtab + obj->symtab[symnum].st_name,
86 obj->path, (void *)*where, defobj->path));
87 break;
89 case R_TYPE(REL32):
90 def = _rtld_find_symdef(symnum, obj, &defobj, false);
91 if (def == NULL)
92 return -1;
94 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
95 rela->r_addend) - (Elf_Addr)where;
96 if (*where != tmp)
97 *where = tmp;
98 rdbg(("PC32 %s in %s --> %p in %s",
99 obj->strtab + obj->symtab[symnum].st_name,
100 obj->path, (void *)*where, defobj->path));
101 break;
102 #endif
104 case R_TYPE(DIR32):
105 def = _rtld_find_symdef(symnum, obj, &defobj, false);
106 if (def == NULL)
107 return -1;
109 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
110 rela->r_addend);
111 if (*where != tmp)
112 *where = tmp;
113 rdbg(("32 %s in %s --> %p in %s",
114 obj->strtab + obj->symtab[symnum].st_name,
115 obj->path, (void *)*where, defobj->path));
116 break;
118 case R_TYPE(GLOB_DAT):
119 def = _rtld_find_symdef(symnum, obj, &defobj, false);
120 if (def == NULL)
121 return -1;
123 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
124 rela->r_addend;
125 if (*where != tmp)
126 *where = tmp;
127 rdbg(("GLOB_DAT %s in %s --> %p in %s",
128 obj->strtab + obj->symtab[symnum].st_name,
129 obj->path, (void *)*where, defobj->path));
130 break;
132 case R_TYPE(RELATIVE):
133 if (rela->r_addend)
134 *where = (Elf_Addr)obj->relocbase + rela->r_addend;
135 else
136 *where += (Elf_Addr)obj->relocbase;
137 rdbg(("RELATIVE in %s --> %p", obj->path,
138 (void *)*where));
139 break;
141 case R_TYPE(COPY):
143 * These are deferred until all other relocations have
144 * been done. All we do here is make sure that the
145 * COPY relocation is not in a shared library. They
146 * are allowed only in executable files.
148 if (obj->isdynamic) {
149 _rtld_error(
150 "%s: Unexpected R_COPY relocation in shared library",
151 obj->path);
152 return -1;
154 rdbg(("COPY (avoid in main)"));
155 break;
157 default:
158 rdbg(("sym = %lu, type = %lu, offset = %p, "
159 "addend = %p, contents = %p, symbol = %s",
160 symnum, (u_long)ELF_R_TYPE(rela->r_info),
161 (void *)rela->r_offset, (void *)rela->r_addend,
162 (void *)*where,
163 obj->strtab + obj->symtab[symnum].st_name));
164 _rtld_error("%s: Unsupported relocation type %ld "
165 "in non-PLT relocations",
166 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
167 return -1;
170 return 0;
174 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
176 const Elf_Rela *rela;
178 if (!obj->relocbase)
179 return 0;
181 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
182 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
184 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
186 /* Just relocate the GOT slots pointing into the PLT */
187 *where += (Elf_Addr)obj->relocbase;
188 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
191 return 0;
194 caddr_t
195 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
197 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
198 Elf_Addr new_value;
199 int err;
201 new_value = 0; /* XXX gcc */
203 err = _rtld_relocate_plt_object(obj, rela, &new_value);
204 if (err)
205 _rtld_die();
207 return (caddr_t)new_value;
211 _rtld_relocate_plt_objects(const Obj_Entry *obj)
213 const Elf_Rela *rela = obj->pltrela;
215 for (; rela < obj->pltrelalim; rela++)
216 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
217 return -1;
219 return 0;
222 static inline int
223 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
225 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
226 Elf_Addr new_value;
227 const Elf_Sym *def;
228 const Obj_Entry *defobj;
229 unsigned long info = rela->r_info;
231 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
233 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
234 if (__predict_false(def == NULL))
235 return -1;
236 if (__predict_false(def == &_rtld_sym_zero))
237 return 0;
239 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
240 rdbg(("bind now/fixup in %s --> old=%p new=%p",
241 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
242 if (*where != new_value)
243 *where = new_value;
245 if (tp)
246 *tp = new_value;
248 return 0;