mdreloc.c revision 1.14 1 /* $NetBSD: mdreloc.c,v 1.14 2002/09/12 22:56:30 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_bind_start(void);
10 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
11
12 void
13 _rtld_setup_pltgot(const Obj_Entry *obj)
14 {
15 obj->pltgot[1] = (Elf_Addr) obj;
16 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
17 }
18
19 void
20 _rtld_relocate_nonplt_self(dynp, relocbase)
21 Elf_Dyn *dynp;
22 Elf_Addr relocbase;
23 {
24 const Elf_Rel *rel = 0, *rellim;
25 Elf_Addr relsz = 0;
26 Elf_Addr *where;
27
28 for (; dynp->d_tag != DT_NULL; dynp++) {
29 switch (dynp->d_tag) {
30 case DT_REL:
31 rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
32 break;
33 case DT_RELSZ:
34 relsz = dynp->d_un.d_val;
35 break;
36 }
37 }
38 rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
39 for (; rel < rellim; rel++) {
40 where = (Elf_Addr *)(relocbase + rel->r_offset);
41 *where += (Elf_Addr)relocbase;
42 }
43 }
44
45 int
46 _rtld_relocate_nonplt_objects(obj, self)
47 const Obj_Entry *obj;
48 bool self;
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(("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(("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(("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(("COPY (avoid in main)"));
142 break;
143
144 default:
145 rdbg(("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)
161 const Obj_Entry *obj;
162 {
163 const Elf_Rel *rel;
164
165 if (!obj->isdynamic)
166 return 0;
167
168 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
169 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
170
171 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
172
173 /* Just relocate the GOT slots pointing into the PLT */
174 *where += (Elf_Addr)obj->relocbase;
175 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
176 }
177
178 return 0;
179 }
180
181 int
182 _rtld_relocate_plt_object(obj, rela, addrp)
183 const Obj_Entry *obj;
184 const Elf_Rela *rela;
185 caddr_t *addrp;
186 {
187 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
188 Elf_Addr new_value;
189 const Elf_Sym *def;
190 const Obj_Entry *defobj;
191
192 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
193
194 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
195 if (def == NULL)
196 return -1;
197
198 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
199 rdbg(("bind now/fixup in %s --> old=%p new=%p",
200 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
201 if (*where != new_value)
202 *where = new_value;
203
204 *addrp = (caddr_t)new_value;
205 return 0;
206 }
207