mips_reloc.c revision 1.11 1 /* $NetBSD: mips_reloc.c,v 1.11 2002/09/05 21:31:33 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(a0, 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(obj->symtabno - i - 1, obj, &defobj,
86 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 unsigned long symnum;
147
148 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
149 symnum = ELF_R_SYM(rel->r_info);
150
151 switch (ELF_R_TYPE(rel->r_info)) {
152 case R_TYPE(NONE):
153 break;
154
155 case R_TYPE(REL32):
156 /* 32-bit PC-relative reference */
157 def = obj->symtab + symnum;
158
159 if (ELF_ST_BIND(def->st_info) == STB_LOCAL &&
160 (ELF_ST_TYPE(def->st_info) == STT_SECTION ||
161 ELF_ST_TYPE(def->st_info) == STT_NOTYPE)) {
162 /*
163 * XXX: ABI DIFFERENCE!
164 *
165 * Old NetBSD binutils would generate shared
166 * libs with section-relative relocations being
167 * already adjusted for the start address of
168 * the section.
169 *
170 * New binutils, OTOH, generate shared libs
171 * with the same relocations being based at
172 * zero, so we need to add in the start address
173 * of the section.
174 *
175 * We assume that all section-relative relocs
176 * with contents less than the start of the
177 * section need to be adjusted; this should
178 * work with both old and new shlibs.
179 *
180 * --rkb, Oct 6, 2001
181 */
182 if (def->st_info == STT_SECTION &&
183 (*where < def->st_value))
184 *where += (Elf_Addr) def->st_value;
185
186 *where += (Elf_Addr)obj->relocbase;
187
188 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
189 obj->strtab + def->st_name, obj->path,
190 (void *)*where, obj->path));
191 } else {
192 /* XXX maybe do something re: bootstrapping? */
193 def = _rtld_find_symdef(symnum, obj, &defobj,
194 false);
195 if (def == NULL)
196 return -1;
197 *where += (Elf_Addr)(defobj->relocbase +
198 def->st_value);
199 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
200 obj->strtab + obj->symtab[symnum].st_name,
201 obj->path, (void *)*where, defobj->path));
202 }
203 break;
204
205 default:
206 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
207 "contents = %p, symbol = %s",
208 symnum, (u_long)ELF_R_TYPE(rel->r_info),
209 (void *)rel->r_offset, (void *)*where,
210 obj->strtab + obj->symtab[symnum].st_name));
211 _rtld_error("%s: Unsupported relocation type %ld "
212 "in non-PLT relocations\n",
213 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
214 return -1;
215 }
216 }
217 return 0;
218 }
219
220 int
221 _rtld_relocate_plt_objects(obj, rela, addrp, bind_now, dodebug)
222 Obj_Entry *obj;
223 const Elf_Rela *rela;
224 caddr_t *addrp;
225 bool bind_now;
226 bool dodebug;
227 {
228 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
229 Elf_Addr new_value;
230
231 /* Fully resolve procedure addresses now */
232
233 if (!obj->mainprog) {
234 /* Just relocate the GOT slots pointing into the PLT */
235 new_value = *where + (Elf_Addr)(obj->relocbase);
236 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
237 (void *)*where));
238 } else {
239 return 0;
240 }
241 /*
242 * Since this page is probably copy-on-write, let's not write
243 * it unless we really really have to.
244 */
245 if (*where != new_value)
246 *where = new_value;
247 if (addrp != NULL) {
248 *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
249 #if defined(__vax__)
250 *addrp -= rela->r_addend;
251 #endif
252 }
253 return 0;
254 }
255