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