mdreloc.c revision 1.9 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 *where += (Elf_Addr)obj->relocbase;
91 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
92 (void *)*where));
93 break;
94
95 case R_TYPE(COPY):
96 /*
97 * These are deferred until all other relocations have
98 * been done. All we do here is make sure that the
99 * COPY relocation is not in a shared library. They
100 * are allowed only in executable files.
101 */
102 if (obj->isdynamic) {
103 _rtld_error(
104 "%s: Unexpected R_COPY relocation in shared library",
105 obj->path);
106 return -1;
107 }
108 rdbg(dodebug, ("COPY (avoid in main)"));
109 break;
110
111 default:
112 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
113 "contents = %p, symbol = %s",
114 symnum, (u_long)ELF_R_TYPE(rel->r_info),
115 (void *)rel->r_offset, (void *)*where,
116 obj->strtab + obj->symtab[symnum].st_name));
117 _rtld_error("%s: Unsupported relocation type %ld "
118 "in non-PLT relocations\n",
119 obj->path, (u_long) ELF_R_TYPE(rel->r_info));
120 return -1;
121 }
122 }
123 return 0;
124 }
125
126 int
127 _rtld_relocate_plt_lazy(obj, dodebug)
128 const Obj_Entry *obj;
129 bool dodebug;
130 {
131 const Elf_Rel *rel;
132
133 if (!obj->isdynamic)
134 return 0;
135
136 for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
137 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
138
139 assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
140
141 /* Just relocate the GOT slots pointing into the PLT */
142 *where += (Elf_Addr)obj->relocbase;
143 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
144 (void *)*where));
145 }
146
147 return 0;
148 }
149
150 int
151 _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
152 const Obj_Entry *obj;
153 const Elf_Rela *rela;
154 caddr_t *addrp;
155 bool dodebug;
156 {
157 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
158 Elf_Addr new_value;
159 const Elf_Sym *def;
160 const Obj_Entry *defobj;
161
162 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
163
164 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
165 if (def == NULL)
166 return -1;
167
168 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
169 rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
170 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
171 if (*where != new_value)
172 *where = new_value;
173
174 *addrp = (caddr_t)new_value;
175 return 0;
176 }
177