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