mkfs: drop support for zone != block
[minix.git] / libexec / ld.elf_so / arch / m68k / mdreloc.c
blobea18f0330c781dece73e4ef0a9723cb97ea3e47e
1 /* $NetBSD: mdreloc.c,v 1.27 2010/08/06 16:33:18 joerg Exp $ */
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.27 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.27 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 *);
25 void
26 _rtld_setup_pltgot(const Obj_Entry *obj)
28 obj->pltgot[1] = (Elf_Addr) obj;
29 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
32 void
33 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
35 const Elf_Rela *rela = 0, *relalim;
36 Elf_Addr relasz = 0;
37 Elf_Addr *where;
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;
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;
56 int
57 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
59 const Elf_Rela *rela;
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;
68 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
69 symnum = ELF_R_SYM(rela->r_info);
71 switch (ELF_R_TYPE(rela->r_info)) {
72 case R_TYPE(NONE):
73 break;
75 #if 1 /* XXX should not occur */
76 case R_TYPE(PC32):
77 def = _rtld_find_symdef(symnum, obj, &defobj, false);
78 if (def == NULL)
79 return -1;
81 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
82 rela->r_addend) - (Elf_Addr)where;
83 if (*where != tmp)
84 *where = tmp;
85 rdbg(("PC32 %s in %s --> %p in %s",
86 obj->strtab + obj->symtab[symnum].st_name,
87 obj->path, (void *)*where, defobj->path));
88 break;
90 case R_TYPE(GOT32):
91 #endif
92 case R_TYPE(32):
93 case R_TYPE(GLOB_DAT):
94 def = _rtld_find_symdef(symnum, obj, &defobj, false);
95 if (def == NULL)
96 return -1;
98 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
99 rela->r_addend);
100 if (*where != tmp)
101 *where = tmp;
102 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
103 obj->strtab + obj->symtab[symnum].st_name,
104 obj->path, (void *)*where, defobj->path));
105 break;
107 case R_TYPE(RELATIVE):
108 *where += (Elf_Addr)obj->relocbase;
109 rdbg(("RELATIVE in %s --> %p", obj->path,
110 (void *)*where));
111 break;
113 case R_TYPE(COPY):
115 * These are deferred until all other relocations have
116 * been done. All we do here is make sure that the
117 * COPY relocation is not in a shared library. They
118 * are allowed only in executable files.
120 if (obj->isdynamic) {
121 _rtld_error(
122 "%s: Unexpected R_COPY relocation in shared library",
123 obj->path);
124 return -1;
126 rdbg(("COPY (avoid in main)"));
127 break;
129 default:
130 rdbg(("sym = %lu, type = %lu, offset = %p, "
131 "addend = %p, contents = %p, symbol = %s",
132 symnum, (u_long)ELF_R_TYPE(rela->r_info),
133 (void *)rela->r_offset, (void *)rela->r_addend,
134 (void *)*where,
135 obj->strtab + obj->symtab[symnum].st_name));
136 _rtld_error("%s: Unsupported relocation type %ld "
137 "in non-PLT relocations",
138 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
139 return -1;
142 return 0;
146 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
148 const Elf_Rela *rela;
150 if (!obj->relocbase)
151 return 0;
153 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
154 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
156 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
158 /* Just relocate the GOT slots pointing into the PLT */
159 *where += (Elf_Addr)obj->relocbase;
160 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
163 return 0;
166 static inline int
167 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
168 Elf_Addr *tp)
170 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
171 Elf_Addr new_value;
172 const Elf_Sym *def;
173 const Obj_Entry *defobj;
174 unsigned long info = rela->r_info;
176 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
178 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
179 if (__predict_false(def == NULL))
180 return -1;
181 if (__predict_false(def == &_rtld_sym_zero))
182 return 0;
184 assert(rela->r_addend == 0);
185 new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
186 rela->r_addend);
187 rdbg(("bind now/fixup in %s --> old=%p new=%p",
188 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
189 if (*where != new_value)
190 *where = new_value;
192 if (tp)
193 *tp = new_value - rela->r_addend;
195 return 0;
198 caddr_t
199 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
201 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
202 Elf_Addr result;
203 int err;
205 result = 0; /* XXX gcc */
207 err = _rtld_relocate_plt_object(obj, rela, &result);
208 if (err)
209 _rtld_die();
211 return (caddr_t)result;
215 _rtld_relocate_plt_objects(const Obj_Entry *obj)
217 const Elf_Rela *rela;
219 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
220 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
221 return -1;
223 return 0;