alpha_reloc.c revision 1.5 1 /* $NetBSD: alpha_reloc.c,v 1.5 2002/09/05 18:25:46 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40
41 #include "rtld.h"
42 #include "debug.h"
43
44 #ifdef RTLD_DEBUG_ALPHA
45 #define adbg(x) if (dodebug) xprintf x
46 #else
47 #define adbg(x) /* nothing */
48 #endif
49
50 void
51 _rtld_setup_pltgot(const Obj_Entry *obj)
52 {
53 uint32_t word0;
54
55 /*
56 * The PLTGOT on the Alpha looks like this:
57 *
58 * PLT HEADER
59 * .
60 * . 32 bytes
61 * .
62 * PLT ENTRY #0
63 * .
64 * . 12 bytes
65 * .
66 * PLT ENTRY #1
67 * .
68 * . 12 bytes
69 * .
70 * etc.
71 *
72 * The old-format entries look like (displacements filled in
73 * by the linker):
74 *
75 * ldah $28, 0($31) # 0x279f0000
76 * lda $28, 0($28) # 0x239c0000
77 * br $31, plt0 # 0xc3e00000
78 *
79 * The new-format entries look like:
80 *
81 * br $28, plt0 # 0xc3800000
82 * # 0x00000000
83 * # 0x00000000
84 *
85 * What we do is fetch the first PLT entry and check to
86 * see the first word of it matches the first word of the
87 * old format. If so, we use a binding routine that can
88 * handle the old format, otherwise we use a binding routine
89 * that handles the new format.
90 *
91 * Note that this is done on a per-object basis, we can mix
92 * and match shared objects build with both the old and new
93 * linker.
94 */
95 word0 = *(uint32_t *)(((char *) obj->pltgot) + 32);
96 if ((word0 & 0xffff0000) == 0x279f0000) {
97 /* Old PLT entry format. */
98 adbg(("ALPHA: object %p has old PLT format\n", obj));
99 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
100 obj->pltgot[3] = (Elf_Addr) obj;
101 } else {
102 /* New PLT entry format. */
103 adbg(("ALPHA: object %p has new PLT format\n", obj));
104 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
105 obj->pltgot[3] = (Elf_Addr) obj;
106 }
107
108 __asm __volatile("imb");
109 }
110
111 int
112 _rtld_relocate_nonplt_object(obj, rela, dodebug)
113 Obj_Entry *obj;
114 const Elf_Rela *rela;
115 bool dodebug;
116 {
117 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
118 const Elf_Sym *def;
119 const Obj_Entry *defobj;
120 Elf_Addr tmp;
121
122 switch (ELF_R_TYPE(rela->r_info)) {
123
124 case R_TYPE(NONE):
125 break;
126
127 case R_TYPE(REFQUAD):
128 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
129 if (def == NULL)
130 return -1;
131
132 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
133 *where + rela->r_addend;
134 if (*where != tmp)
135 *where = tmp;
136 rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
137 defobj->strtab + def->st_name, obj->path,
138 (void *)*where, defobj->path));
139 break;
140
141 case R_TYPE(GLOB_DAT):
142 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
143 if (def == NULL)
144 return -1;
145
146 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
147 rela->r_addend;
148 if (*where != tmp)
149 *where = tmp;
150 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
151 defobj->strtab + def->st_name, obj->path,
152 (void *)*where, defobj->path));
153 break;
154
155 case R_TYPE(RELATIVE):
156 {
157 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
158 extern Elf_Addr _GOT_END_[];
159
160 /* This is the ...iffy hueristic. */
161 if (!dodebug ||
162 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
163 (caddr_t)where >= (caddr_t)_GOT_END_) {
164 *where += (Elf_Addr)obj->relocbase;
165 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
166 (void *)*where));
167 } else
168 rdbg(dodebug, ("RELATIVE in %s stays at %p",
169 obj->path, (void *)*where));
170 break;
171 }
172
173 case R_TYPE(COPY):
174 /*
175 * These are deferred until all other relocations have
176 * been done. All we do here is make sure that the COPY
177 * relocation is not in a shared library. They are allowed
178 * only in executable files.
179 */
180 if (!obj->mainprog) {
181 _rtld_error(
182 "%s: Unexpected R_COPY relocation in shared library",
183 obj->path);
184 return -1;
185 }
186 rdbg(dodebug, ("COPY (avoid in main)"));
187 break;
188
189 default:
190 def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
191 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
192 "addend = %p, contents = %p, symbol = %s",
193 (u_long)ELF_R_SYM(rela->r_info),
194 (u_long)ELF_R_TYPE(rela->r_info),
195 (void *)rela->r_offset, (void *)rela->r_addend,
196 (void *)*where,
197 def ? defobj->strtab + def->st_name : "??"));
198 _rtld_error("%s: Unsupported relocation type %ld "
199 "in non-PLT relocations\n",
200 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
201 return -1;
202 }
203 return 0;
204 }
205