mdreloc.c revision 1.12 1 /* $NetBSD: mdreloc.c,v 1.12 2002/09/11 17:23:25 mycroft Exp $ */
2
3 #include <sys/types.h>
4 #include <sys/stat.h>
5
6 #include "debug.h"
7 #include "rtld.h"
8
9 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
10
11 void
12 _rtld_setup_pltgot(const Obj_Entry *obj)
13 {
14 obj->pltgot[1] = (Elf_Addr) obj;
15 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
16 }
17
18 void
19 _rtld_relocate_nonplt_self(dynp, relocbase)
20 Elf_Dyn *dynp;
21 Elf_Addr relocbase;
22 {
23 const Elf_Rel *rel = 0, *rellim;
24 Elf_Addr relsz = 0;
25 Elf_Addr *where;
26
27 for (; dynp->d_tag != DT_NULL; dynp++) {
28 switch (dynp->d_tag) {
29 case DT_REL:
30 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
31 break;
32 case DT_RELSZ:
33 relsz = dynp->d_un.d_val;
34 break;
35 }
36 }
37 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
38 for (; rel < rellim; rel++) {
39 where = (Elf_Addr *)(relocbase + rel->r_offset);
40 *where += (Elf_Addr)relocbase;
41 }
42 }
43
44 int
45 _rtld_relocate_nonplt_objects(obj, self, dodebug)
46 const Obj_Entry *obj;
47 bool self;
48 bool dodebug;
49 {
50 const Elf_Rel *rel;
51 #define COMBRELOC
52 #ifdef COMBRELOC
53 unsigned long lastsym = -1;
54 #endif
55 Elf_Addr target;
56
57 if (self)
58 return 0;
59
60 for (rel = obj->rel; rel < obj->rellim; rel++) {
61 Elf_Addr *where;
62 const Elf_Sym *def;
63 const Obj_Entry *defobj;
64 Elf_Addr tmp;
65 unsigned long symnum;
66
67 where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
68 symnum = ELF_R_SYM(rel->r_info);
69
70 switch (ELF_R_TYPE(rel->r_info)) {
71 case R_TYPE(NONE):
72 break;
73
74 #if 1 /* XXX should not occur */
75 case R_TYPE(PC32):
76 #ifdef COMBRELOC
77 if (symnum != lastsym) {
78 #endif
79 def = _rtld_find_symdef(symnum, obj, &defobj,
80 false);
81 if (def == NULL)
82 return -1;
83 target = (Elf_Addr)(defobj->relocbase +
84 def->st_value);
85 #ifdef COMBRELOC
86 lastsym = symnum;
87 }
88 #endif
89
90 *where += target - (Elf_Addr)where;
91 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
92 obj->strtab + obj->symtab[symnum].st_name,
93 obj->path, (void *)*where, defobj->path));
94 break;
95
96 case R_TYPE(GOT32):
97 #endif
98 case R_TYPE(32):
99 case R_TYPE(GLOB_DAT):
100 #ifdef COMBRELOC
101 if (symnum != lastsym) {
102 #endif
103 def = _rtld_find_symdef(symnum, obj, &defobj,
104 false);
105 if (def == NULL)
106 return -1;
107 target = (Elf_Addr)(defobj->relocbase +
108 def->st_value);
109 #ifdef COMBRELOC
110 lastsym = symnum;
111 }
112 #endif
113
114 tmp = target + *where;
115 if (*where != tmp)
116 *where = tmp;
117 rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
118 obj->strtab + obj->symtab[symnum].st_name,
119 obj->path, (void *)*where, defobj->path));
120 break;
121
122 case R_TYPE(RELATIVE):
123 *where += (Elf_Addr)obj->relocbase;
124 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
125 (void *)*where));
126 break;
127
128 case R_TYPE(COPY):
129 /*
130 * These are deferred until all other relocations have
131 * been done. All we do here is make sure that the
132 * COPY relocation is not in a shared library. They
133 * are allowed only in executable files.
134 */
135 if (obj->isdynamic) {
136 _rtld_error(
137 "%s: Unexpected R_COPY relocation in shared library",
138 obj->path);
139 return -1;
140 }
141 rdbg(dodebug, ("COPY (avoid in main)"));
142 break;
143
144 default:
145 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
146 "contents = %p, symbol = %s",
147 symnum, (u_long)ELF_R_TYPE(rel->r_info),
148 (void *)rel->r_offset, (void *)*where,
149 obj->strtab + obj->symtab[symnum].st_name));
150 _rtld_error("%s: Unsupported relocation type %ld "
151 "in non-PLT relocations\n",
152 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
153 return -1;
154 }
155 }
156 return 0;
157 }
158
159 int
160 _rtld_relocate_plt_lazy(obj, dodebug)
161 const Obj_Entry *obj;
162 bool dodebug;
163 {
164 const Elf_Rel *rel;
165
166 if (!obj->isdynamic)
167 return 0;
168
169 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
170 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
171
172 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
173
174 /* Just relocate the GOT slots pointing into the PLT */
175 *where += (Elf_Addr)obj->relocbase;
176 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
177 (void *)*where));
178 }
179
180 return 0;
181 }
182
183 int
184 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
185 const Obj_Entry *obj;
186 const Elf_Rela *rela;
187 caddr_t *addrp;
188 bool dodebug;
189 {
190 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
191 Elf_Addr new_value;
192 const Elf_Sym *def;
193 const Obj_Entry *defobj;
194
195 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
196
197 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
198 if (def == NULL)
199 return -1;
200
201 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
202 rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
203 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
204 if (*where != new_value)
205 *where = new_value;
206
207 *addrp = (caddr_t)new_value;
208 return 0;
209 }
210