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