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