mips_reloc.c revision 1.9 1 /* $NetBSD: mips_reloc.c,v 1.9 2002/09/05 20:08:17 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
42 caddr_t
43 _rtld_bind_mips(a0, a1, a2, a3)
44 Elf_Word a0;
45 Elf_Addr a1, a2, a3;
46 {
47 Elf_Addr *u = (Elf_Addr *)(a2 - 0x7ff0);
48 Obj_Entry *obj = (Obj_Entry *)(u[1] & 0x7fffffff);
49 const Elf_Sym *def;
50 const Obj_Entry *defobj;
51
52 def = _rtld_find_symdef(ELF_R_INFO(a0, 0), obj, &defobj, true);
53 if (def) {
54 u[obj->local_gotno + a0 - obj->gotsym] = (Elf_Addr)
55 (def->st_value + defobj->relocbase);
56 return((caddr_t)(def->st_value + defobj->relocbase));
57 }
58
59 return(NULL); /* XXX */
60 }
61
62 void
63 _rtld_setup_pltgot(obj)
64 Obj_Entry *obj;
65 {
66 Elf_Addr *got = obj->pltgot;
67 const Elf_Sym *sym = obj->symtab;
68 const Elf_Sym *def;
69 const Obj_Entry *defobj;
70 int i;
71
72 i = (got[1] & 0x80000000) ? 2 : 1;
73 /* Relocate the local GOT entries */
74 while (i < obj->local_gotno)
75 got[i++] += (Elf_Word)obj->relocbase;
76 got += obj->local_gotno;
77 i = obj->symtabno - obj->gotsym;
78 sym += obj->gotsym;
79 /* Now do the global GOT entries */
80 while (i--) {
81 rdbg(1, (" doing got %d sym %p (%s, %x)",
82 obj->symtabno - obj->gotsym - i - 1,
83 sym, sym->st_name + obj->strtab, *got));
84
85 def = _rtld_find_symdef(ELF_R_INFO(obj->symtabno - i - 1, 0),
86 obj, &defobj, true);
87 if (def == NULL)
88 _rtld_error(
89 "%s: Undefined PLT symbol \"%s\" (section type = %ld, symnum = %ld)",
90 obj->path, sym->st_name + obj->strtab,
91 (u_long) ELF_ST_TYPE(sym->st_info),
92 (u_long) obj->symtabno - i - 1);
93 else {
94
95 if (sym->st_shndx == SHN_UNDEF) {
96 #if 0 /* These don't seem to work? */
97
98 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC) {
99 if (sym->st_value)
100 *got = sym->st_value +
101 (Elf_Word)obj->relocbase;
102 else
103 *got = def->st_value +
104 (Elf_Word)defobj->relocbase;
105 } else
106 #endif
107 *got = def->st_value +
108 (Elf_Word)defobj->relocbase;
109 } else if (sym->st_shndx == SHN_COMMON) {
110 *got = def->st_value +
111 (Elf_Word)defobj->relocbase;
112 } else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
113 *got != sym->st_value) {
114 *got += (Elf_Word)obj->relocbase;
115 } else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION &&
116 ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
117 if (sym->st_shndx == SHN_ABS)
118 *got = sym->st_value +
119 (Elf_Word)obj->relocbase;
120 /* else SGI stuff ignored */
121 } else
122 *got = def->st_value +
123 (Elf_Word)defobj->relocbase;
124 }
125
126 ++sym;
127 ++got;
128 }
129
130 obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
131 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
132 obj->pltgot[1] |= (Elf_Addr) obj;
133 }
134
135 int
136 _rtld_relocate_nonplt_objects(obj, dodebug)
137 Obj_Entry *obj;
138 bool dodebug;
139 {
140 const Elf_Rel *rel;
141
142 for (rel = obj->rel; rel < obj->rellim; rel++) {
143 Elf_Addr *where;
144 const Elf_Sym *def;
145 const Obj_Entry *defobj;
146
147 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
148
149 switch (ELF_R_TYPE(rel->r_info)) {
150 case R_TYPE(NONE):
151 break;
152
153 case R_TYPE(REL32):
154 /* 32-bit PC-relative reference */
155 def = obj->symtab + ELF_R_SYM(rel->r_info);
156
157 if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
158 (ELF_ST_TYPE(def->st_info) == STT_SECTION ||
159 ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
160 /*
161 * XXX: ABI DIFFERENCE!
162 *
163 * Old NetBSD binutils would generate shared
164 * libs with section-relative relocations being
165 * already adjusted for the start address of
166 * the section.
167 *
168 * New binutils, OTOH, generate shared libs
169 * with the same relocations being based at
170 * zero, so we need to add in the start address
171 * of the section.
172 *
173 * We assume that all section-relative relocs
174 * with contents less than the start of the
175 * section need to be adjusted; this should
176 * work with both old and new shlibs.
177 *
178 * --rkb, Oct 6, 2001
179 */
180 if (def->st_info == STT_SECTION &&
181 (*where < def->st_value))
182 *where += (Elf_Addr) def->st_value;
183
184 *where += (Elf_Addr)obj->relocbase;
185
186 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
187 obj->strtab + def->st_name, obj->path,
188 (void *)*where, obj->path));
189 } else {
190 /* XXX maybe do something re: bootstrapping? */
191 def = _rtld_find_symdef(rel->r_info, obj,
192 &defobj, false);
193 if (def == NULL)
194 return -1;
195 *where += (Elf_Addr)(defobj->relocbase +
196 def->st_value);
197 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
198 defobj->strtab + def->st_name, obj->path,
199 (void *)*where, defobj->path));
200 }
201 break;
202
203 default:
204 def = _rtld_find_symdef(rel->r_info, obj, &defobj,
205 true);
206 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
207 "contents = %p, symbol = %s",
208 (u_long)ELF_R_SYM(rel->r_info),
209 (u_long)ELF_R_TYPE(rel->r_info),
210 (void *)rel->r_offset, (void *)*where,
211 def ? defobj->strtab + def->st_name : "??"));
212 _rtld_error("%s: Unsupported relocation type %ld "
213 "in non-PLT relocations\n",
214 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
215 return -1;
216 }
217 }
218 return 0;
219 }
220
221 int
222 _rtld_relocate_plt_objects(obj, rela, addrp, bind_now, dodebug)
223 Obj_Entry *obj;
224 const Elf_Rela *rela;
225 caddr_t *addrp;
226 bool bind_now;
227 bool dodebug;
228 {
229 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
230 Elf_Addr new_value;
231
232 /* Fully resolve procedure addresses now */
233
234 if (!obj->mainprog) {
235 /* Just relocate the GOT slots pointing into the PLT */
236 new_value = *where + (Elf_Addr)(obj->relocbase);
237 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
238 (void *)*where));
239 } else {
240 return 0;
241 }
242 /*
243 * Since this page is probably copy-on-write, let's not write
244 * it unless we really really have to.
245 */
246 if (*where != new_value)
247 *where = new_value;
248 if (addrp != NULL) {
249 *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
250 #if defined(__vax__)
251 *addrp -= rela->r_addend;
252 #endif
253 }
254 return 0;
255 }
256