Sync usage with man page.
[netbsd-mini2440.git] / libexec / ld.elf_so / arch / sh3 / mdreloc.c
blob3601d2c74b077dcad310582754fd69c16ddfc073
1 /* $NetBSD: mdreloc.c,v 1.25 2009/03/16 02:46:47 lukem Exp $ */
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.25 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.25 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 *);
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 #if 1 /* XXX should not occur */
76 case R_TYPE(GOT32):
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);
83 if (*where != tmp)
84 *where = tmp;
85 rdbg(("GOT32 %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(REL32):
91 def = _rtld_find_symdef(symnum, obj, &defobj, false);
92 if (def == NULL)
93 return -1;
95 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
96 rela->r_addend) - (Elf_Addr)where;
97 if (*where != tmp)
98 *where = tmp;
99 rdbg(("PC32 %s in %s --> %p in %s",
100 obj->strtab + obj->symtab[symnum].st_name,
101 obj->path, (void *)*where, defobj->path));
102 break;
103 #endif
105 case R_TYPE(DIR32):
106 def = _rtld_find_symdef(symnum, obj, &defobj, false);
107 if (def == NULL)
108 return -1;
110 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
111 rela->r_addend);
112 if (*where != tmp)
113 *where = tmp;
114 rdbg(("32 %s in %s --> %p in %s",
115 obj->strtab + obj->symtab[symnum].st_name,
116 obj->path, (void *)*where, defobj->path));
117 break;
119 case R_TYPE(GLOB_DAT):
120 def = _rtld_find_symdef(symnum, obj, &defobj, false);
121 if (def == NULL)
122 return -1;
124 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
125 rela->r_addend;
126 if (*where != tmp)
127 *where = tmp;
128 rdbg(("GLOB_DAT %s in %s --> %p in %s",
129 obj->strtab + obj->symtab[symnum].st_name,
130 obj->path, (void *)*where, defobj->path));
131 break;
133 case R_TYPE(RELATIVE):
134 if (rela->r_addend)
135 *where = (Elf_Addr)obj->relocbase + rela->r_addend;
136 else
137 *where += (Elf_Addr)obj->relocbase;
138 rdbg(("RELATIVE in %s --> %p", obj->path,
139 (void *)*where));
140 break;
142 case R_TYPE(COPY):
144 * These are deferred until all other relocations have
145 * been done. All we do here is make sure that the
146 * COPY relocation is not in a shared library. They
147 * are allowed only in executable files.
149 if (obj->isdynamic) {
150 _rtld_error(
151 "%s: Unexpected R_COPY relocation in shared library",
152 obj->path);
153 return -1;
155 rdbg(("COPY (avoid in main)"));
156 break;
158 default:
159 rdbg(("sym = %lu, type = %lu, offset = %p, "
160 "addend = %p, contents = %p, symbol = %s",
161 symnum, (u_long)ELF_R_TYPE(rela->r_info),
162 (void *)rela->r_offset, (void *)rela->r_addend,
163 (void *)*where,
164 obj->strtab + obj->symtab[symnum].st_name));
165 _rtld_error("%s: Unsupported relocation type %ld "
166 "in non-PLT relocations",
167 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
168 return -1;
171 return 0;
175 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
177 const Elf_Rela *rela;
179 if (!obj->relocbase)
180 return 0;
182 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
183 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
185 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
187 /* Just relocate the GOT slots pointing into the PLT */
188 *where += (Elf_Addr)obj->relocbase;
189 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
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 new_value;
200 int err;
202 new_value = 0; /* XXX gcc */
204 err = _rtld_relocate_plt_object(obj, rela, &new_value);
205 if (err || new_value == 0)
206 _rtld_die();
208 return (caddr_t)new_value;
212 _rtld_relocate_plt_objects(const Obj_Entry *obj)
214 const Elf_Rela *rela = obj->pltrela;
216 for (; rela < obj->pltrelalim; rela++)
217 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
218 return -1;
220 return 0;
223 static inline int
224 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
226 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
227 Elf_Addr new_value;
228 const Elf_Sym *def;
229 const Obj_Entry *defobj;
231 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
233 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
234 if (def == NULL)
235 return -1;
237 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
238 rdbg(("bind now/fixup in %s --> old=%p new=%p",
239 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
240 if (*where != new_value)
241 *where = new_value;
243 if (tp)
244 *tp = new_value;
246 return 0;