usr.sbin/makefs: Minor fix / cleanup
[dragonfly.git] / stand / boot / common / reloc_elf.c
blobe55c838a994c4e1cc5d3fab38790858f00cbebac
1 /*-
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>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
27 * SUCH DAMAGE.
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>
35 #include <errno.h>
36 #include <stand.h>
38 #define FREEBSD_ELF
39 #include <link.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.
51 int
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)
55 #if (defined(__x86_64__) || defined(__i386__)) && __ELF_WORD_SIZE == 64
56 Elf64_Addr *where, val;
57 Elf_Addr addend, addr;
58 Elf_Size rtype, symidx;
59 const Elf_Rel *rel;
60 const Elf_Rela *rela;
62 switch (reltype) {
63 case ELF_RELOC_REL:
64 rel = (const Elf_Rel *)reldata;
65 where = (Elf64_Addr *)((char *)data + relbase + rel->r_offset -
66 dataaddr);
67 addend = 0;
68 rtype = ELF_R_TYPE(rel->r_info);
69 symidx = ELF_R_SYM(rel->r_info);
70 addend = 0;
71 break;
72 case ELF_RELOC_RELA:
73 rela = (const Elf_Rela *)reldata;
74 where = (Elf64_Addr *)((char *)data + relbase + rela->r_offset -
75 dataaddr);
76 addend = rela->r_addend;
77 rtype = ELF_R_TYPE(rela->r_info);
78 symidx = ELF_R_SYM(rela->r_info);
79 break;
80 default:
81 return (EINVAL);
84 if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
85 return (0);
87 if (reltype == ELF_RELOC_REL)
88 addend = *where;
90 /* XXX, definitions not available on i386. */
91 #define R_X86_64_64 1
92 #define R_X86_64_RELATIVE 8
94 switch (rtype) {
95 case R_X86_64_64: /* S + A */
96 addr = symaddr(ef, symidx);
97 if (addr == 0)
98 return (ESRCH);
99 val = addr + addend;
100 *where = val;
101 break;
102 case R_X86_64_RELATIVE:
103 addr = (Elf_Addr)addend + relbase;
104 val = addr;
105 *where = val;
106 break;
107 default:
108 printf("\nunhandled relocation type %u\n", (u_int)rtype);
109 return (EFTYPE);
112 return (0);
113 #elif defined(__i386__) && __ELF_WORD_SIZE == 32
114 Elf_Addr addend, addr, *where, val;
115 Elf_Size rtype, symidx;
116 const Elf_Rel *rel;
117 const Elf_Rela *rela;
119 switch (reltype) {
120 case ELF_RELOC_REL:
121 rel = (const Elf_Rel *)reldata;
122 where = (Elf_Addr *)((char *)data + relbase + rel->r_offset -
123 dataaddr);
124 addend = 0;
125 rtype = ELF_R_TYPE(rel->r_info);
126 symidx = ELF_R_SYM(rel->r_info);
127 addend = 0;
128 break;
129 case ELF_RELOC_RELA:
130 rela = (const Elf_Rela *)reldata;
131 where = (Elf_Addr *)((char *)data + relbase + rela->r_offset -
132 dataaddr);
133 addend = rela->r_addend;
134 rtype = ELF_R_TYPE(rela->r_info);
135 symidx = ELF_R_SYM(rela->r_info);
136 break;
137 default:
138 return (EINVAL);
141 if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
142 return (0);
144 if (reltype == ELF_RELOC_REL)
145 addend = *where;
147 /* XXX, definitions not available on x86_64. */
148 #define R_386_32 1 /* Add symbol value. */
149 #define R_386_GLOB_DAT 6 /* Set GOT entry to data address. */
150 #define R_386_RELATIVE 8 /* Add load address of shared object. */
152 switch (rtype) {
153 case R_386_RELATIVE:
154 addr = addend + relbase;
155 *where = addr;
156 break;
157 case R_386_32: /* S + A */
158 addr = symaddr(ef, symidx);
159 if (addr == 0)
160 return (ESRCH);
161 val = addr + addend;
162 *where = val;
163 break;
164 default:
165 printf("\nunhandled relocation type %u\n", (u_int)rtype);
166 return (EFTYPE);
169 return (0);
170 #else
171 return (EOPNOTSUPP);
172 #endif