No empty .Rs/.Re
[netbsd-mini2440.git] / libexec / ld.elf_so / arch / m68k / mdreloc.c
blobafc2eddb5b7b619870567a1ab9811d824ce095e6
1 /* $NetBSD: mdreloc.c,v 1.23 2009/03/16 02:46:47 lukem Exp $ */
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.23 2009/03/16 02:46:47 lukem Exp $");
6 #endif /* not lint */
8 #include <sys/cdefs.h>
9 #ifndef lint
10 __RCSID("$NetBSD: mdreloc.c,v 1.23 2009/03/16 02:46:47 lukem Exp $");
11 #endif /* not lint */
13 #include <sys/types.h>
14 #include <sys/stat.h>
16 #include "debug.h"
17 #include "rtld.h"
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 *);
26 void
27 _rtld_setup_pltgot(const Obj_Entry *obj)
29 obj->pltgot[1] = (Elf_Addr) obj;
30 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
33 void
34 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
36 const Elf_Rela *rela = 0, *relalim;
37 Elf_Addr relasz = 0;
38 Elf_Addr *where;
40 for (; dynp->d_tag != DT_NULL; dynp++) {
41 switch (dynp->d_tag) {
42 case DT_RELA:
43 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
44 break;
45 case DT_RELASZ:
46 relasz = dynp->d_un.d_val;
47 break;
50 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
51 for (; rela < relalim; rela++) {
52 where = (Elf_Addr *)(relocbase + rela->r_offset);
53 *where += (Elf_Addr)relocbase;
57 int
58 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
60 const Elf_Rela *rela;
62 for (rela = obj->rela; rela < obj->relalim; rela++) {
63 Elf_Addr *where;
64 const Elf_Sym *def;
65 const Obj_Entry *defobj;
66 Elf_Addr tmp;
67 unsigned long symnum;
69 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
70 symnum = ELF_R_SYM(rela->r_info);
72 switch (ELF_R_TYPE(rela->r_info)) {
73 case R_TYPE(NONE):
74 break;
76 #if 1 /* XXX should not occur */
77 case R_TYPE(PC32):
78 def = _rtld_find_symdef(symnum, obj, &defobj, false);
79 if (def == NULL)
80 return -1;
82 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
83 rela->r_addend) - (Elf_Addr)where;
84 if (*where != tmp)
85 *where = tmp;
86 rdbg(("PC32 %s in %s --> %p in %s",
87 obj->strtab + obj->symtab[symnum].st_name,
88 obj->path, (void *)*where, defobj->path));
89 break;
91 case R_TYPE(GOT32):
92 #endif
93 case R_TYPE(32):
94 case R_TYPE(GLOB_DAT):
95 def = _rtld_find_symdef(symnum, obj, &defobj, false);
96 if (def == NULL)
97 return -1;
99 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
100 rela->r_addend);
101 if (*where != tmp)
102 *where = tmp;
103 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
104 obj->strtab + obj->symtab[symnum].st_name,
105 obj->path, (void *)*where, defobj->path));
106 break;
108 case R_TYPE(RELATIVE):
109 *where += (Elf_Addr)obj->relocbase;
110 rdbg(("RELATIVE in %s --> %p", obj->path,
111 (void *)*where));
112 break;
114 case R_TYPE(COPY):
116 * These are deferred until all other relocations have
117 * been done. All we do here is make sure that the
118 * COPY relocation is not in a shared library. They
119 * are allowed only in executable files.
121 if (obj->isdynamic) {
122 _rtld_error(
123 "%s: Unexpected R_COPY relocation in shared library",
124 obj->path);
125 return -1;
127 rdbg(("COPY (avoid in main)"));
128 break;
130 default:
131 rdbg(("sym = %lu, type = %lu, offset = %p, "
132 "addend = %p, contents = %p, symbol = %s",
133 symnum, (u_long)ELF_R_TYPE(rela->r_info),
134 (void *)rela->r_offset, (void *)rela->r_addend,
135 (void *)*where,
136 obj->strtab + obj->symtab[symnum].st_name));
137 _rtld_error("%s: Unsupported relocation type %ld "
138 "in non-PLT relocations",
139 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
140 return -1;
143 return 0;
147 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
149 const Elf_Rela *rela;
151 if (!obj->relocbase)
152 return 0;
154 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
155 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
157 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
159 /* Just relocate the GOT slots pointing into the PLT */
160 *where += (Elf_Addr)obj->relocbase;
161 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
164 return 0;
167 static inline int
168 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, 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;
175 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
177 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
178 if (def == NULL)
179 return -1;
181 assert(rela->r_addend == 0);
182 new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
183 rela->r_addend);
184 rdbg(("bind now/fixup in %s --> old=%p new=%p",
185 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
186 if (*where != new_value)
187 *where = new_value;
189 if (tp)
190 *tp = new_value - rela->r_addend;
192 return 0;
195 caddr_t
196 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
198 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
199 Elf_Addr result;
200 int err;
202 result = 0; /* XXX gcc */
204 err = _rtld_relocate_plt_object(obj, rela, &result);
205 if (err || result == 0)
206 _rtld_die();
208 return (caddr_t)result;
212 _rtld_relocate_plt_objects(const Obj_Entry *obj)
214 const Elf_Rela *rela;
216 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
217 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
218 return -1;
220 return 0;