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