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
29 * $FreeBSD: src/sys/boot/common/reloc_elf.c,v 1.2 2005/12/18 04:52:35 marcel Exp $
32 #include <sys/types.h>
33 #include <machine/elf.h>
41 #include "bootstrap.h"
43 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
46 * Apply a single intra-module relocation to the data. `relbase' is the
47 * target relocation base for the section (i.e. it corresponds to where
48 * r_offset == 0). `dataaddr' is the relocated address corresponding to
49 * the start of the data, and `len' is the number of bytes.
52 __elfN(reloc
)(struct elf_file
*ef
, symaddr_fn
*symaddr
, const void *reldata
,
53 int reltype
, Elf_Addr relbase
, Elf_Addr dataaddr
, void *data
, size_t len
)
62 if (relbase
+ a
->r_offset
>= dataaddr
&&
63 relbase
+ a
->r_offset
< dataaddr
+ len
) {
64 switch (ELF_R_TYPE(a
->r_info
)) {
65 case R_SPARC_RELATIVE
:
66 w
= relbase
+ a
->r_addend
;
67 bcopy(&w
, (u_char
*)data
+ (relbase
+
68 a
->r_offset
- dataaddr
), sizeof(w
));
71 printf("\nunhandled relocation type %u\n",
72 (u_int
)ELF_R_TYPE(a
->r_info
));
80 #elif defined(__i386__) && __ELF_WORD_SIZE == 64
81 Elf64_Addr
*where
, val
;
82 Elf_Addr addend
, addr
;
83 Elf_Size rtype
, symidx
;
89 rel
= (const Elf_Rel
*)reldata
;
90 where
= (Elf_Addr
*)((char *)data
+ relbase
+ rel
->r_offset
-
93 rtype
= ELF_R_TYPE(rel
->r_info
);
94 symidx
= ELF_R_SYM(rel
->r_info
);
98 rela
= (const Elf_Rela
*)reldata
;
99 where
= (Elf_Addr
*)((char *)data
+ relbase
+ rela
->r_offset
-
101 addend
= rela
->r_addend
;
102 rtype
= ELF_R_TYPE(rela
->r_info
);
103 symidx
= ELF_R_SYM(rela
->r_info
);
109 if ((char *)where
< (char *)data
|| (char *)where
>= (char *)data
+ len
)
112 if (reltype
== ELF_RELOC_REL
)
115 /* XXX, definitions not available on i386. */
116 #define R_X86_64_64 1
117 #define R_X86_64_RELATIVE 8
120 case R_X86_64_64
: /* S + A */
121 addr
= symaddr(ef
, symidx
);
127 case R_X86_64_RELATIVE
:
128 addr
= (Elf_Addr
)addend
+ relbase
;
133 printf("\nunhandled relocation type %u\n", (u_int
)rtype
);
138 #elif defined(__i386__) && __ELF_WORD_SIZE == 32
139 Elf_Addr addend
, addr
, *where
, val
;
140 Elf_Size rtype
, symidx
;
142 const Elf_Rela
*rela
;
146 rel
= (const Elf_Rel
*)reldata
;
147 where
= (Elf_Addr
*)((char *)data
+ relbase
+ rel
->r_offset
-
150 rtype
= ELF_R_TYPE(rel
->r_info
);
151 symidx
= ELF_R_SYM(rel
->r_info
);
155 rela
= (const Elf_Rela
*)reldata
;
156 where
= (Elf_Addr
*)((char *)data
+ relbase
+ rela
->r_offset
-
158 addend
= rela
->r_addend
;
159 rtype
= ELF_R_TYPE(rela
->r_info
);
160 symidx
= ELF_R_SYM(rela
->r_info
);
166 if ((char *)where
< (char *)data
|| (char *)where
>= (char *)data
+ len
)
169 if (reltype
== ELF_RELOC_REL
)
172 /* XXX, definitions not available on x86_64. */
173 #define R_386_32 1 /* Add symbol value. */
174 #define R_386_GLOB_DAT 6 /* Set GOT entry to data address. */
175 #define R_386_RELATIVE 8 /* Add load address of shared object. */
179 addr
= addend
+ relbase
;
182 case R_386_32
: /* S + A */
183 addr
= symaddr(ef
, symidx
);
190 printf("\nunhandled relocation type %u\n", (u_int
)rtype
);