1 /* $NetBSD: mdreloc.c,v 1.1 2014/09/03 19:34:26 matt Exp $ */
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: mdreloc.c,v 1.1 2014/09/03 19:34:26 matt Exp $");
41 #include <sys/types.h>
42 #include <machine/cpu.h>
47 void _rtld_bind_start(void);
48 Elf_Addr
_rtld_bind(const Obj_Entry
*, Elf_Word
);
49 void _rtld_relocate_nonplt_self(Elf_Dyn
*, Elf_Addr
);
50 static int _rtld_relocate_plt_object(const Obj_Entry
*,
51 const Elf_Rela
*, int, Elf_Addr
*);
54 * The OR1k PLT format is simple.
58 _rtld_setup_pltgot(const Obj_Entry
*obj
)
60 obj
->pltgot
[1] = (Elf_Addr
) obj
;
61 obj
->pltgot
[2] = (Elf_Addr
) _rtld_bind_start
;
63 dbg(("obj %s plt pltgot=%p start=%p obj=%p",
64 obj
->path
, obj
->pltgot
,
65 (void *) obj
->pltgot
[1], (void *) obj
->pltgot
[2]));
69 _rtld_relocate_nonplt_self(Elf_Dyn
*dynp
, Elf_Addr relocbase
)
71 const Elf_Rela
*rela
= 0, *relalim
;
75 for (; dynp
->d_tag
!= DT_NULL
; dynp
++) {
76 switch (dynp
->d_tag
) {
78 rela
= (const Elf_Rela
*)(relocbase
+ dynp
->d_un
.d_ptr
);
81 relasz
= dynp
->d_un
.d_val
;
85 relalim
= (const Elf_Rela
*)((const uint8_t *)rela
+ relasz
);
86 for (; rela
< relalim
; rela
++) {
87 where
= (Elf_Addr
*)(relocbase
+ rela
->r_offset
);
88 *where
= (Elf_Addr
)(relocbase
+ rela
->r_addend
);
93 _rtld_relocate_nonplt_objects(Obj_Entry
*obj
)
97 for (rela
= obj
->rela
; rela
< obj
->relalim
; rela
++) {
100 const Obj_Entry
*defobj
;
102 unsigned long symnum
;
104 where
= (Elf_Addr
*)(obj
->relocbase
+ rela
->r_offset
);
105 symnum
= ELF_R_SYM(rela
->r_info
);
107 switch (ELF_R_TYPE(rela
->r_info
)) {
108 #if 1 /* XXX Should not be necessary. */
109 case R_TYPE(JMP_SLOT
):
114 case R_TYPE(32): /* <address> S + A */
115 case R_TYPE(GLOB_DAT
): /* <address> S + A */
116 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
120 tmp
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
+
124 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
125 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
126 obj
->path
, (void *)*where
, defobj
->path
));
129 case R_TYPE(RELATIVE
): /* <address> B + A */
130 *where
= (Elf_Addr
)(obj
->relocbase
+ rela
->r_addend
);
131 rdbg(("RELATIVE in %s --> %p", obj
->path
,
137 * These are deferred until all other relocations have
138 * been done. All we do here is make sure that the
139 * COPY relocation is not in a shared library. They
140 * are allowed only in executable files.
142 if (obj
->isdynamic
) {
144 "%s: Unexpected R_COPY relocation in shared library",
148 rdbg(("COPY (avoid in main)"));
151 case R_TYPE(TLS_DTPMOD
):
152 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
156 *where
= (Elf_Addr
)defobj
->tlsindex
;
157 rdbg(("DTPMOD32 %s in %s --> %p in %s",
158 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
159 obj
->path
, (void *)*where
, defobj
->path
));
162 case R_TYPE(TLS_DTPOFF
):
163 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
167 if (!defobj
->tls_done
&& _rtld_tls_offset_allocate(obj
))
170 *where
= (Elf_Addr
)(def
->st_value
+ rela
->r_addend
172 rdbg(("DTPOFF %s in %s --> %p in %s",
173 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
174 obj
->path
, (void *)*where
, defobj
->path
));
177 case R_TYPE(TLS_TPOFF
):
178 def
= _rtld_find_symdef(symnum
, obj
, &defobj
, false);
182 if (!defobj
->tls_done
&& _rtld_tls_offset_allocate(obj
))
185 *where
= (Elf_Addr
)(def
->st_value
+ rela
->r_addend
186 + defobj
->tlsoffset
- TLS_TP_OFFSET
);
187 rdbg(("TPREL %s in %s --> %p in %s",
188 obj
->strtab
+ obj
->symtab
[symnum
].st_name
,
189 obj
->path
, (void *)*where
, defobj
->path
));
193 rdbg(("sym = %lu, type = %lu, offset = %p, "
194 "addend = %p, contents = %p, symbol = %s",
195 symnum
, (u_long
)ELF_R_TYPE(rela
->r_info
),
196 (void *)rela
->r_offset
, (void *)rela
->r_addend
,
198 obj
->strtab
+ obj
->symtab
[symnum
].st_name
));
199 _rtld_error("%s: Unsupported relocation type %ld "
200 "in non-PLT relocations",
201 obj
->path
, (u_long
) ELF_R_TYPE(rela
->r_info
));
209 _rtld_relocate_plt_lazy(const Obj_Entry
*obj
)
211 const Elf_Rela
*rela
;
214 for (rela
= obj
->pltrela
, reloff
= 0;
215 rela
< obj
->pltrelalim
;
217 Elf_Word
*where
= (Elf_Word
*)(obj
->relocbase
+ rela
->r_offset
);
219 assert(ELF_R_TYPE(rela
->r_info
) == R_TYPE(JMP_SLOT
));
222 * For now, simply treat then as relative.
224 *where
+= (Elf_Addr
)obj
->relocbase
;
231 _rtld_relocate_plt_object(const Obj_Entry
*obj
, const Elf_Rela
*rela
, int reloff
, Elf_Addr
*tp
)
233 Elf_Word
*where
= (Elf_Word
*)(obj
->relocbase
+ rela
->r_offset
);
236 const Obj_Entry
*defobj
;
237 unsigned long info
= rela
->r_info
;
239 assert(ELF_R_TYPE(info
) == R_TYPE(JMP_SLOT
));
241 def
= _rtld_find_plt_symdef(ELF_R_SYM(info
), obj
, &defobj
, tp
!= NULL
);
242 if (__predict_false(def
== NULL
))
244 if (__predict_false(def
== &_rtld_sym_zero
))
247 value
= (Elf_Addr
)(defobj
->relocbase
+ def
->st_value
);
248 rdbg(("bind now/fixup in %s --> new=%p",
249 defobj
->strtab
+ def
->st_name
, (void *)value
));
252 * Simply replace the entry in GOT with the
253 * address of the routine.
255 assert(where
>= (Elf_Word
*)obj
->pltgot
);
256 assert(where
< (Elf_Word
*)obj
->pltgot
+ (obj
->pltrelalim
- obj
->pltrela
));
265 _rtld_bind(const Obj_Entry
*obj
, Elf_Word reloff
)
267 const Elf_Rela
*rela
= obj
->pltrela
+ reloff
;
271 new_value
= 0; /* XXX gcc */
273 _rtld_shared_enter();
274 err
= _rtld_relocate_plt_object(obj
, rela
, reloff
, &new_value
);
283 _rtld_relocate_plt_objects(const Obj_Entry
*obj
)
285 const Elf_Rela
*rela
;
288 for (rela
= obj
->pltrela
, reloff
= 0; rela
< obj
->pltrelalim
; rela
++, reloff
++) {
289 if (_rtld_relocate_plt_object(obj
, rela
, reloff
, NULL
) < 0)