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