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