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