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