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