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