mips_reloc.c revision 1.26 1 /* $NetBSD: mips_reloc.c,v 1.26 2002/09/13 16:03:20 mycroft Exp $ */
2
3 /*
4 * Copyright 1997 Michael L. Hitch <mhitch (at) montana.edu>
5 * Portions copyright 2002 Charles M. Hannum.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include "debug.h"
37 #include "rtld.h"
38
39 void _rtld_bind_start(void);
40 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
41 caddr_t _rtld_bind_mips(Elf_Word, Elf_Addr, Elf_Addr, Elf_Addr);
42
43 caddr_t
44 _rtld_bind_mips(a0, a1, a2, a3)
45 Elf_Word a0;
46 Elf_Addr a1, a2, a3;
47 {
48 Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
49 const Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
50 const Elf_Sym *def;
51 const Obj_Entry *defobj;
52 Elf_Addr new_value;
53
54 def = _rtld_find_symdef(a0, obj, &defobj, true);
55 if (def == NULL)
56 return(NULL); /* XXX */
57
58 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
59 rdbg(("bind now/fixup in %s --> new=%p",
60 defobj->strtab + def->st_name, (void *)new_value));
61 u[obj->local_gotno + a0 - obj->gotsym] = new_value;
62 return ((caddr_t)new_value);
63 }
64
65 void
66 _rtld_setup_pltgot(obj)
67 const Obj_Entry *obj;
68 {
69 obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
70 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
71 obj->pltgot[1] |= (Elf_Addr) obj;
72 }
73
74 void
75 _rtld_relocate_nonplt_self(dynp, relocbase)
76 Elf_Dyn *dynp;
77 Elf_Addr relocbase;
78 {
79 const Elf_Rel *rel = 0, *rellim;
80 Elf_Addr relsz = 0;
81 Elf_Addr *where;
82 const Elf_Sym *symtab, *sym;
83 Elf_Addr *got;
84 Elf_Word local_gotno, symtabno, gotsym;
85 int i;
86
87 for (; dynp->d_tag != DT_NULL; dynp++) {
88 switch (dynp->d_tag) {
89 case DT_REL:
90 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
91 break;
92 case DT_RELSZ:
93 relsz = dynp->d_un.d_val;
94 break;
95 case DT_SYMTAB:
96 symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
97 break;
98 case DT_PLTGOT:
99 got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
100 break;
101 case DT_MIPS_LOCAL_GOTNO:
102 local_gotno = dynp->d_un.d_val;
103 break;
104 case DT_MIPS_SYMTABNO:
105 symtabno = dynp->d_un.d_val;
106 break;
107 case DT_MIPS_GOTSYM:
108 gotsym = dynp->d_un.d_val;
109 break;
110 }
111 }
112 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
113 for (; rel < rellim; rel++) {
114 where = (Elf_Addr *)(relocbase + rel->r_offset);
115
116 switch (ELF_R_TYPE(rel->r_info)) {
117 case R_TYPE(NONE):
118 break;
119
120 case R_TYPE(REL32):
121 sym = symtab + ELF_R_SYM(rel->r_info);
122 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL &&
123 ELF_ST_TYPE(sym->st_info) == STT_SECTION)
124 *where += (Elf_Addr)(sym->st_value + relocbase);
125 else
126 abort();
127 break;
128
129 default:
130 abort();
131 }
132 }
133
134 i = (got[1] & 0x80000000) ? 2 : 1;
135 /* Relocate the local GOT entries */
136 got += i;
137 for (; i < local_gotno; i++)
138 *got++ += relocbase;
139 sym = symtab + gotsym;
140 /* Now do the global GOT entries */
141 for (i = gotsym; i < symtabno; i++) {
142 if (sym->st_shndx == SHN_UNDEF ||
143 sym->st_shndx == SHN_COMMON)
144 *got = sym->st_value + relocbase;
145 else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
146 *got += relocbase;
147 else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
148 ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
149 if (sym->st_shndx == SHN_ABS)
150 *got = sym->st_value + relocbase;
151 /* else SGI stuff ignored */
152 } else
153 *got = sym->st_value + relocbase;
154 ++sym;
155 ++got;
156 }
157 }
158
159 int
160 _rtld_relocate_nonplt_objects(obj, self)
161 const Obj_Entry *obj;
162 bool self;
163 {
164 const Elf_Rel *rel;
165 Elf_Addr *got = obj->pltgot;
166 const Elf_Sym *sym, *def;
167 const Obj_Entry *defobj;
168 int i;
169
170 if (self)
171 return 0;
172
173 for (rel = obj->rel; rel < obj->rellim; rel++) {
174 Elf_Addr *where;
175 unsigned long symnum;
176
177 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
178 symnum = ELF_R_SYM(rel->r_info);
179
180 switch (ELF_R_TYPE(rel->r_info)) {
181 case R_TYPE(NONE):
182 break;
183
184 case R_TYPE(REL32):
185 /* 32-bit PC-relative reference */
186 def = obj->symtab + symnum;
187
188 if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
189 (ELF_ST_TYPE(def->st_info) == STT_SECTION ||
190 ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
191 /*
192 * XXX: ABI DIFFERENCE!
193 *
194 * Old NetBSD binutils would generate shared
195 * libs with section-relative relocations being
196 * already adjusted for the start address of
197 * the section.
198 *
199 * New binutils, OTOH, generate shared libs
200 * with the same relocations being based at
201 * zero, so we need to add in the start address
202 * of the section.
203 *
204 * We assume that all section-relative relocs
205 * with contents less than the start of the
206 * section need to be adjusted; this should
207 * work with both old and new shlibs.
208 *
209 * --rkb, Oct 6, 2001
210 */
211 if (def->st_info == STT_SECTION &&
212 *where < def->st_value)
213 *where += (Elf_Addr)def->st_value;
214
215 *where += (Elf_Addr)obj->relocbase;
216
217 rdbg(("REL32 %s in %s --> %p in %s",
218 obj->strtab + def->st_name, obj->path,
219 (void *)*where, obj->path));
220 } else {
221 def = _rtld_find_symdef(symnum, obj, &defobj,
222 false);
223 if (def == NULL)
224 return -1;
225 *where += (Elf_Addr)(defobj->relocbase +
226 def->st_value);
227 rdbg(("REL32 %s in %s --> %p in %s",
228 obj->strtab + obj->symtab[symnum].st_name,
229 obj->path, (void *)*where, defobj->path));
230 }
231 break;
232
233 default:
234 rdbg(("sym = %lu, type = %lu, offset = %p, "
235 "contents = %p, symbol = %s",
236 symnum, (u_long)ELF_R_TYPE(rel->r_info),
237 (void *)rel->r_offset, (void *)*where,
238 obj->strtab + obj->symtab[symnum].st_name));
239 _rtld_error("%s: Unsupported relocation type %ld "
240 "in non-PLT relocations\n",
241 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
242 return -1;
243 }
244 }
245
246 i = (got[1] & 0x80000000) ? 2 : 1;
247 /* Relocate the local GOT entries */
248 got += i;
249 for (; i < obj->local_gotno; i++)
250 *got++ += (Elf_Addr)obj->relocbase;
251 sym = obj->symtab + obj->gotsym;
252 /* Now do the global GOT entries */
253 for (i = obj->gotsym; i < obj->symtabno; i++) {
254 rdbg((" doing got %d sym %p (%s, %x)", i - obj->gotsym, sym,
255 sym->st_name + obj->strtab, *got));
256
257 if (sym->st_shndx == SHN_UNDEF &&
258 ELF_ST_BIND(sym->st_info) != STB_WEAK &&
259 ELF_ST_TYPE(sym->st_info) == STT_FUNC)
260 *got = sym->st_value + (Elf_Addr)obj->relocbase;
261 else if (sym->st_shndx == SHN_UNDEF ||
262 sym->st_shndx == SHN_COMMON) {
263 def = _rtld_find_symdef(i, obj, &defobj, true);
264 if (def == NULL)
265 return -1;
266 *got = def->st_value + (Elf_Addr)defobj->relocbase;
267 } else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
268 *got += (Elf_Addr)obj->relocbase;
269 else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
270 ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
271 if (sym->st_shndx == SHN_ABS)
272 *got = sym->st_value +
273 (Elf_Addr)obj->relocbase;
274 /* else SGI stuff ignored */
275 } else {
276 def = _rtld_find_symdef(i, obj, &defobj, true);
277 if (def == NULL)
278 return -1;
279 *got = def->st_value + (Elf_Addr)defobj->relocbase;
280 }
281 ++sym;
282 ++got;
283 }
284
285 return 0;
286 }
287
288 int
289 _rtld_relocate_plt_lazy(obj)
290 const Obj_Entry *obj;
291 {
292 }
293