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