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