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