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