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