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