No empty .Rs/.Re
[netbsd-mini2440.git] / libexec / ld.elf_so / arch / vax / mdreloc.c
blobc2202f5d948684ac5b264b14d093e148ae726531
1 /* $NetBSD: mdreloc.c,v 1.24 2009/03/16 02:46:48 lukem Exp $ */
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.24 2009/03/16 02:46:48 lukem Exp $");
6 #endif /* not lint */
8 #include <sys/cdefs.h>
9 #ifndef lint
10 __RCSID("$NetBSD: mdreloc.c,v 1.24 2009/03/16 02:46:48 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 *);
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 + rela->r_addend);
56 int
57 _rtld_relocate_nonplt_objects(const 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 case R_TYPE(32): /* word32 S + A */
76 case R_TYPE(GLOB_DAT): /* word32 S + A */
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);
84 if (*where != tmp)
85 *where = tmp;
86 rdbg(("32/GLOB_DAT %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(RELATIVE): /* word32 B + A */
92 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
93 if (*where != tmp)
94 *where = tmp;
95 rdbg(("RELATIVE in %s --> %p", obj->path,
96 (void *)*where));
97 break;
99 case R_TYPE(COPY):
101 * These are deferred until all other relocations have
102 * been done. All we do here is make sure that the
103 * COPY relocation is not in a shared library. They
104 * are allowed only in executable files.
106 if (obj->isdynamic) {
107 _rtld_error(
108 "%s: Unexpected R_COPY relocation in shared library",
109 obj->path);
110 return -1;
112 rdbg(("COPY (avoid in main)"));
113 break;
115 default:
116 rdbg(("sym = %lu, type = %lu, offset = %p, "
117 "addend = %p, contents = %p, symbol = %s",
118 symnum, (u_long)ELF_R_TYPE(rela->r_info),
119 (void *)rela->r_offset, (void *)rela->r_addend,
120 (void *)*where,
121 obj->strtab + obj->symtab[symnum].st_name));
122 _rtld_error("%s: Unsupported relocation type %ld "
123 "in non-PLT relocations",
124 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
125 return -1;
128 return 0;
132 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
134 const Elf_Rela *rela;
136 if (!obj->relocbase)
137 return 0;
139 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
140 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
142 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
144 /* Just relocate the GOT slots pointing into the PLT */
145 *where += (Elf_Addr)obj->relocbase;
146 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
149 return 0;
152 static inline int
153 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
155 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
156 Elf_Addr new_value;
157 const Elf_Sym *def;
158 const Obj_Entry *defobj;
160 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
162 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
163 if (def == NULL)
164 return -1;
166 new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
167 rela->r_addend);
168 rdbg(("bind now/fixup in %s --> old=%p new=%p",
169 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
170 if (*where != new_value)
171 *where = new_value;
173 if (tp)
174 *tp = new_value - rela->r_addend;
176 return 0;
179 caddr_t
180 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
182 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
183 Elf_Addr result;
184 int err;
186 result = 0; /* XXX gcc */
188 err = _rtld_relocate_plt_object(obj, rela, &result);
189 if (err || result == 0)
190 _rtld_die();
192 return (caddr_t)result;
196 _rtld_relocate_plt_objects(const Obj_Entry *obj)
198 const Elf_Rela *rela;
200 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
201 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
202 return -1;
204 return 0;