2 * Copyright (c) 2003 Jake Burkholder.
3 * Copyright 1996-1998 John D. Polstra.
4 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
31 #include <machine/elf.h>
35 #include <sys/link_elf.h>
37 #include "bootstrap.h"
39 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
42 * Apply a single intra-module relocation to the data. `relbase' is the
43 * target relocation base for the section (i.e. it corresponds to where
44 * r_offset == 0). `dataaddr' is the relocated address corresponding to
45 * the start of the data, and `len' is the number of bytes.
48 __elfN(reloc
)(struct elf_file
*ef
, symaddr_fn
*symaddr
, const void *reldata
,
49 int reltype
, Elf_Addr relbase
, Elf_Addr dataaddr
, void *data
, size_t len
)
51 #if (defined(__aarch64__) || defined(__amd64__) || defined(__i386__)) && \
53 Elf64_Addr
*where
, val
;
54 Elf_Addr addend
, addr
;
56 #if defined(__amd64__) || defined(__i386__)
64 rel
= (const Elf_Rel
*)reldata
;
65 where
= (Elf_Addr
*)((char *)data
+ relbase
+ rel
->r_offset
-
68 rtype
= ELF_R_TYPE(rel
->r_info
);
69 #if defined(__amd64__) || defined(__i386__)
70 symidx
= ELF_R_SYM(rel
->r_info
);
75 rela
= (const Elf_Rela
*)reldata
;
76 where
= (Elf_Addr
*)((char *)data
+ relbase
+ rela
->r_offset
-
78 addend
= rela
->r_addend
;
79 rtype
= ELF_R_TYPE(rela
->r_info
);
80 #if defined(__amd64__) || defined(__i386__)
81 symidx
= ELF_R_SYM(rela
->r_info
);
88 if ((char *)where
< (char *)data
|| (char *)where
>= (char *)data
+ len
)
91 if (reltype
== ELF_RELOC_REL
)
94 #if defined(__aarch64__)
95 #define RELOC_RELATIVE R_AARCH64_RELATIVE
96 #define RELOC_IRELATIVE R_AARCH64_IRELATIVE
97 #elif defined(__amd64__) || defined(__i386__)
98 /* XXX, definitions not available on i386. */
100 #define R_X86_64_RELATIVE 8
101 #define R_X86_64_IRELATIVE 37
103 #define RELOC_RELATIVE R_X86_64_RELATIVE
104 #define RELOC_IRELATIVE R_X86_64_IRELATIVE
109 addr
= (Elf_Addr
)addend
+ relbase
;
111 memcpy(where
, &val
, sizeof(val
));
113 case RELOC_IRELATIVE
:
114 /* leave it to kernel */
116 #if defined(__amd64__) || defined(__i386__)
117 case R_X86_64_64
: /* S + A */
118 addr
= symaddr(ef
, symidx
);
126 printf("\nunhandled relocation type %u\n", (u_int
)rtype
);
131 #elif defined(__i386__) && __ELF_WORD_SIZE == 32
132 Elf_Addr addend
, addr
, *where
, val
;
133 Elf_Size rtype
, symidx
;
135 const Elf_Rela
*rela
;
139 rel
= (const Elf_Rel
*)reldata
;
140 where
= (Elf_Addr
*)((char *)data
+ relbase
+ rel
->r_offset
-
143 rtype
= ELF_R_TYPE(rel
->r_info
);
144 symidx
= ELF_R_SYM(rel
->r_info
);
148 rela
= (const Elf_Rela
*)reldata
;
149 where
= (Elf_Addr
*)((char *)data
+ relbase
+ rela
->r_offset
-
151 addend
= rela
->r_addend
;
152 rtype
= ELF_R_TYPE(rela
->r_info
);
153 symidx
= ELF_R_SYM(rela
->r_info
);
159 if ((char *)where
< (char *)data
|| (char *)where
>= (char *)data
+ len
)
162 if (reltype
== ELF_RELOC_REL
)
165 /* XXX, definitions not available on amd64. */
166 #define R_386_32 1 /* Add symbol value. */
167 #define R_386_GLOB_DAT 6 /* Set GOT entry to data address. */
168 #define R_386_RELATIVE 8 /* Add load address of shared object. */
169 #define R_386_IRELATIVE 42
173 addr
= addend
+ relbase
;
176 case R_386_32
: /* S + A */
177 addr
= symaddr(ef
, symidx
);
183 case R_386_IRELATIVE
:
184 /* leave it to kernel */
187 printf("\nunhandled relocation type %u\n", (u_int
)rtype
);
192 #elif defined(__powerpc__) || defined(__riscv)
194 const Elf_Rela
*rela
;
199 if (relbase
+ rela
->r_offset
>= dataaddr
&&
200 relbase
+ rela
->r_offset
< dataaddr
+ len
) {
201 switch (ELF_R_TYPE(rela
->r_info
)) {
202 #if defined(__powerpc__)
204 #elif defined(__riscv)
205 case R_RISCV_RELATIVE
:
207 w
= relbase
+ rela
->r_addend
;
208 bcopy(&w
, (u_char
*)data
+ (relbase
+
209 rela
->r_offset
- dataaddr
), sizeof(w
));
212 printf("\nunhandled relocation type %u\n",
213 (u_int
)ELF_R_TYPE(rela
->r_info
));