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