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