mdreloc.c revision 1.17 1 /* $NetBSD: mdreloc.c,v 1.17 2003/07/24 10:12:30 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 + rela->r_addend);
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 case R_TYPE(32): /* word32 S + A */
64 case R_TYPE(GLOB_DAT): /* word32 S + A */
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);
71
72 if (*where != tmp)
73 *where = tmp;
74 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
75 obj->strtab + obj->symtab[symnum].st_name,
76 obj->path, (void *)*where, defobj->path));
77 break;
78
79 case R_TYPE(RELATIVE): /* word32 B + A */
80 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
81 if (*where != tmp)
82 *where = tmp;
83 rdbg(("RELATIVE in %s --> %p", obj->path,
84 (void *)*where));
85 break;
86
87 case R_TYPE(COPY):
88 /*
89 * These are deferred until all other relocations have
90 * been done. All we do here is make sure that the
91 * COPY relocation is not in a shared library. They
92 * are allowed only in executable files.
93 */
94 if (obj->isdynamic) {
95 _rtld_error(
96 "%s: Unexpected R_COPY relocation in shared library",
97 obj->path);
98 return -1;
99 }
100 rdbg(("COPY (avoid in main)"));
101 break;
102
103 default:
104 rdbg(("sym = %lu, type = %lu, offset = %p, "
105 "addend = %p, contents = %p, symbol = %s",
106 symnum, (u_long)ELF_R_TYPE(rela->r_info),
107 (void *)rela->r_offset, (void *)rela->r_addend,
108 (void *)*where,
109 obj->strtab + obj->symtab[symnum].st_name));
110 _rtld_error("%s: Unsupported relocation type %ld "
111 "in non-PLT relocations\n",
112 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
113 return -1;
114 }
115 }
116 return 0;
117 }
118
119 int
120 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
121 {
122 const Elf_Rela *rela;
123
124 if (!obj->relocbase)
125 return 0;
126
127 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
128 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
129
130 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
131
132 /* Just relocate the GOT slots pointing into the PLT */
133 *where += (Elf_Addr)obj->relocbase;
134 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
135 }
136
137 return 0;
138 }
139
140 caddr_t
141 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
142 {
143 const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
144 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
145 Elf_Addr new_value;
146 const Elf_Sym *def;
147 const Obj_Entry *defobj;
148
149 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
150
151 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
152 if (def == NULL)
153 _rtld_die();
154
155 new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
156 rela->r_addend);
157 rdbg(("bind now/fixup in %s --> old=%p new=%p",
158 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
159 if (*where != new_value)
160 *where = new_value;
161
162 return (caddr_t)(new_value - rela->r_addend);
163 }
164