mdreloc.c revision 1.14 1 /* $NetBSD: mdreloc.c,v 1.14 2002/09/26 20:42:12 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 caddr_t _rtld_bind __P((const Obj_Entry *, Elf_Word));
11
12 void
13 _rtld_setup_pltgot(const Obj_Entry *obj)
14 {
15 obj->pltgot[1] = (Elf_Addr) obj;
16 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
17 }
18
19 int
20 _rtld_relocate_nonplt_objects(obj)
21 const Obj_Entry *obj;
22 {
23 const Elf_Rela *rela;
24
25 for (rela = obj->rela; rela < obj->relalim; rela++) {
26 Elf_Addr *where;
27 const Elf_Sym *def;
28 const Obj_Entry *defobj;
29 Elf_Addr tmp;
30 unsigned long symnum;
31
32 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
33 symnum = ELF_R_SYM(rela->r_info);
34
35 switch (ELF_R_TYPE(rela->r_info)) {
36 case R_TYPE(NONE):
37 break;
38
39 case R_TYPE(32): /* word32 S + A */
40 case R_TYPE(GLOB_DAT): /* word32 S + A */
41 def = _rtld_find_symdef(symnum, obj, &defobj, false);
42 if (def == NULL)
43 return -1;
44
45 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
46 rela->r_addend);
47
48 if (*where != tmp)
49 *where = tmp;
50 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
51 obj->strtab + obj->symtab[symnum].st_name,
52 obj->path, (void *)*where, defobj->path));
53 break;
54
55 case R_TYPE(RELATIVE): /* word32 B + A */
56 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
57 if (*where != tmp)
58 *where = tmp;
59 rdbg(("RELATIVE in %s --> %p", obj->path,
60 (void *)*where));
61 break;
62
63 case R_TYPE(COPY):
64 /*
65 * These are deferred until all other relocations have
66 * been done. All we do here is make sure that the
67 * COPY relocation is not in a shared library. They
68 * are allowed only in executable files.
69 */
70 if (obj->isdynamic) {
71 _rtld_error(
72 "%s: Unexpected R_COPY relocation in shared library",
73 obj->path);
74 return -1;
75 }
76 rdbg(("COPY (avoid in main)"));
77 break;
78
79 default:
80 rdbg(("sym = %lu, type = %lu, offset = %p, "
81 "addend = %p, contents = %p, symbol = %s",
82 symnum, (u_long)ELF_R_TYPE(rela->r_info),
83 (void *)rela->r_offset, (void *)rela->r_addend,
84 (void *)*where,
85 obj->strtab + obj->symtab[symnum].st_name));
86 _rtld_error("%s: Unsupported relocation type %ld "
87 "in non-PLT relocations\n",
88 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
89 return -1;
90 }
91 }
92 return 0;
93 }
94
95 int
96 _rtld_relocate_plt_lazy(obj)
97 const Obj_Entry *obj;
98 {
99 const Elf_Rela *rela;
100
101 if (!obj->isdynamic)
102 return 0;
103
104 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
105 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
106
107 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
108
109 /* Just relocate the GOT slots pointing into the PLT */
110 *where += (Elf_Addr)obj->relocbase;
111 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
112 }
113
114 return 0;
115 }
116
117 caddr_t
118 _rtld_bind(obj, reloff)
119 const Obj_Entry *obj;
120 Elf_Word reloff;
121 {
122 const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
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 _rtld_die();
133
134 new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
135 rela->r_addend);
136 rdbg(("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 return (caddr_t)(new_value - rela->r_addend);
142 }
143