1199767fToomas Soome/*-
2199767fToomas Soome * Copyright (c) 2003 Jake Burkholder.
3199767fToomas Soome * Copyright 1996-1998 John D. Polstra.
4199767fToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5199767fToomas Soome * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
6199767fToomas Soome * All rights reserved.
7199767fToomas Soome *
8199767fToomas Soome * Redistribution and use in source and binary forms, with or without
9199767fToomas Soome * modification, are permitted provided that the following conditions
10199767fToomas Soome * are met:
11199767fToomas Soome * 1. Redistributions of source code must retain the above copyright
12199767fToomas Soome *    notice, this list of conditions and the following disclaimer.
13199767fToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
14199767fToomas Soome *    notice, this list of conditions and the following disclaimer in the
15199767fToomas Soome *    documentation and/or other materials provided with the distribution.
16199767fToomas Soome *
17199767fToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18199767fToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19199767fToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20199767fToomas Soome * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21199767fToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22199767fToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23199767fToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24199767fToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25199767fToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26199767fToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27199767fToomas Soome * SUCH DAMAGE.
28199767fToomas Soome */
29199767fToomas Soome
30199767fToomas Soome#include <sys/cdefs.h>
31199767fToomas Soome__FBSDID("$FreeBSD$");
32199767fToomas Soome
33199767fToomas Soome#include <sys/types.h>
34199767fToomas Soome#include <machine/elf.h>
35199767fToomas Soome
36199767fToomas Soome#include <stand.h>
37199767fToomas Soome
38199767fToomas Soome#define FREEBSD_ELF
39199767fToomas Soome#include <link.h>
40199767fToomas Soome
41199767fToomas Soome#include "bootstrap.h"
42199767fToomas Soome
43199767fToomas Soome#define COPYOUT(s,d,l)	archsw.arch_copyout((vm_offset_t)(s), d, l)
44199767fToomas Soome
45199767fToomas Soome/*
46199767fToomas Soome * Apply a single intra-module relocation to the data. `relbase' is the
47199767fToomas Soome * target relocation base for the section (i.e. it corresponds to where
48199767fToomas Soome * r_offset == 0). `dataaddr' is the relocated address corresponding to
49199767fToomas Soome * the start of the data, and `len' is the number of bytes.
50199767fToomas Soome */
51199767fToomas Soomeint
52199767fToomas Soome__elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata,
53199767fToomas Soome    int reltype, Elf_Addr relbase, Elf_Addr dataaddr, void *data, size_t len)
54199767fToomas Soome{
55199767fToomas Soome#ifdef __sparc__
56199767fToomas Soome	Elf_Size w;
57199767fToomas Soome	const Elf_Rela *a;
58199767fToomas Soome
59199767fToomas Soome	switch (reltype) {
60199767fToomas Soome	case ELF_RELOC_RELA:
61199767fToomas Soome		a = reldata;
62199767fToomas Soome		 if (relbase + a->r_offset >= dataaddr &&
63199767fToomas Soome		     relbase + a->r_offset < dataaddr + len) {
64199767fToomas Soome			switch (ELF_R_TYPE(a->r_info)) {
65199767fToomas Soome			case R_SPARC_RELATIVE:
66199767fToomas Soome				w = relbase + a->r_addend;
67199767fToomas Soome				bcopy(&w, (u_char *)data + (relbase +
68199767fToomas Soome				    a->r_offset - dataaddr), sizeof(w));
69199767fToomas Soome				break;
70199767fToomas Soome			default:
71199767fToomas Soome				printf("\nunhandled relocation type %u\n",
72199767fToomas Soome				    (u_int)ELF_R_TYPE(a->r_info));
73199767fToomas Soome				return (EFTYPE);
74199767fToomas Soome			}
75199767fToomas Soome		}
76199767fToomas Soome		break;
77199767fToomas Soome	}
78199767fToomas Soome
79199767fToomas Soome	return (0);
80199767fToomas Soome#elif (defined(__i386__) || defined(__amd64__)) && __ELF_WORD_SIZE == 64
81199767fToomas Soome	Elf64_Addr *where, val;
82199767fToomas Soome	Elf_Addr addend, addr;
83199767fToomas Soome	Elf_Size rtype, symidx;
84199767fToomas Soome	const Elf_Rel *rel;
85199767fToomas Soome	const Elf_Rela *rela;
86199767fToomas Soome
87199767fToomas Soome	switch (reltype) {
88199767fToomas Soome	case ELF_RELOC_REL:
89199767fToomas Soome		rel = (const Elf_Rel *)reldata;
90199767fToomas Soome		where = (Elf_Addr *)((char *)data + relbase + rel->r_offset -
91199767fToomas Soome		    dataaddr);
92199767fToomas Soome		addend = 0;
93199767fToomas Soome		rtype = ELF_R_TYPE(rel->r_info);
94199767fToomas Soome		symidx = ELF_R_SYM(rel->r_info);
95199767fToomas Soome		addend = 0;
96199767fToomas Soome		break;
97199767fToomas Soome	case ELF_RELOC_RELA:
98199767fToomas Soome		rela = (const Elf_Rela *)reldata;
99199767fToomas Soome		where = (Elf_Addr *)((char *)data + relbase + rela->r_offset -
100199767fToomas Soome		    dataaddr);
101199767fToomas Soome		addend = rela->r_addend;
102199767fToomas Soome		rtype = ELF_R_TYPE(rela->r_info);
103199767fToomas Soome		symidx = ELF_R_SYM(rela->r_info);
104199767fToomas Soome		break;
105199767fToomas Soome	default:
106199767fToomas Soome		return (EINVAL);
107199767fToomas Soome	}
108199767fToomas Soome
109199767fToomas Soome	if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
110199767fToomas Soome		return (0);
111199767fToomas Soome
112199767fToomas Soome	if (reltype == ELF_RELOC_REL)
113199767fToomas Soome		addend = *where;
114199767fToomas Soome
115199767fToomas Soome/* XXX, definitions not available on i386. */
116199767fToomas Soome#define	R_X86_64_64		1
117199767fToomas Soome#define	R_X86_64_RELATIVE	8
118199767fToomas Soome
119199767fToomas Soome	switch (rtype) {
120199767fToomas Soome	case R_X86_64_64:		/* S + A */
121199767fToomas Soome		addr = symaddr(ef, symidx);
122199767fToomas Soome		if (addr == 0)
123199767fToomas Soome			return (ESRCH);
124199767fToomas Soome		val = addr + addend;
125199767fToomas Soome		*where = val;
126199767fToomas Soome		break;
127199767fToomas Soome	case R_X86_64_RELATIVE:
128199767fToomas Soome		addr = (Elf_Addr)addend + relbase;
129199767fToomas Soome		val = addr;
130199767fToomas Soome		*where = val;
131199767fToomas Soome		break;
132199767fToomas Soome	default:
133199767fToomas Soome		printf("\nunhandled relocation type %u\n", (u_int)rtype);
134199767fToomas Soome		return (EFTYPE);
135199767fToomas Soome	}
136199767fToomas Soome
137199767fToomas Soome	return (0);
138199767fToomas Soome#elif defined(__i386__) && __ELF_WORD_SIZE == 32
139199767fToomas Soome	Elf_Addr addend, addr, *where, val;
140199767fToomas Soome	Elf_Size rtype, symidx;
141199767fToomas Soome	const Elf_Rel *rel;
142199767fToomas Soome	const Elf_Rela *rela;
143199767fToomas Soome
144199767fToomas Soome	switch (reltype) {
145199767fToomas Soome	case ELF_RELOC_REL:
146199767fToomas Soome		rel = (const Elf_Rel *)reldata;
147199767fToomas Soome		where = (Elf_Addr *)((char *)data + relbase + rel->r_offset -
148199767fToomas Soome		    dataaddr);
149199767fToomas Soome		addend = 0;
150199767fToomas Soome		rtype = ELF_R_TYPE(rel->r_info);
151199767fToomas Soome		symidx = ELF_R_SYM(rel->r_info);
152199767fToomas Soome		addend = 0;
153199767fToomas Soome		break;
154199767fToomas Soome	case ELF_RELOC_RELA:
155199767fToomas Soome		rela = (const Elf_Rela *)reldata;
156199767fToomas Soome		where = (Elf_Addr *)((char *)data + relbase + rela->r_offset -
157199767fToomas Soome		    dataaddr);
158199767fToomas Soome		addend = rela->r_addend;
159199767fToomas Soome		rtype = ELF_R_TYPE(rela->r_info);
160199767fToomas Soome		symidx = ELF_R_SYM(rela->r_info);
161199767fToomas Soome		break;
162199767fToomas Soome	default:
163199767fToomas Soome		return (EINVAL);
164199767fToomas Soome	}
165199767fToomas Soome
166199767fToomas Soome	if ((char *)where < (char *)data || (char *)where >= (char *)data + len)
167199767fToomas Soome		return (0);
168199767fToomas Soome
169199767fToomas Soome	if (reltype == ELF_RELOC_REL)
170199767fToomas Soome		addend = *where;
171199767fToomas Soome
172199767fToomas Soome/* XXX, definitions not available on amd64. */
173199767fToomas Soome#define R_386_32	1	/* Add symbol value. */
174199767fToomas Soome#define R_386_GLOB_DAT	6	/* Set GOT entry to data address. */
175199767fToomas Soome#define R_386_RELATIVE	8	/* Add load address of shared object. */
176199767fToomas Soome
177199767fToomas Soome	switch (rtype) {
178199767fToomas Soome	case R_386_RELATIVE:
179199767fToomas Soome		addr = addend + relbase;
180199767fToomas Soome		*where = addr;
181199767fToomas Soome		break;
182199767fToomas Soome	case R_386_32:		/* S + A */
183199767fToomas Soome		addr = symaddr(ef, symidx);
184199767fToomas Soome		if (addr == 0)
185199767fToomas Soome			return (ESRCH);
186199767fToomas Soome		val = addr + addend;
187199767fToomas Soome		*where = val;
188199767fToomas Soome		break;
189199767fToomas Soome	default:
190199767fToomas Soome		printf("\nunhandled relocation type %u\n", (u_int)rtype);
191199767fToomas Soome		return (EFTYPE);
192199767fToomas Soome	}
193199767fToomas Soome
194199767fToomas Soome	return (0);
195199767fToomas Soome#elif defined(__powerpc__)
196199767fToomas Soome	Elf_Size w;
197199767fToomas Soome	const Elf_Rela *rela;
198199767fToomas Soome
199199767fToomas Soome	switch (reltype) {
200199767fToomas Soome	case ELF_RELOC_RELA:
201199767fToomas Soome		rela = reldata;
202199767fToomas Soome		if (relbase + rela->r_offset >= dataaddr &&
203199767fToomas Soome		    relbase + rela->r_offset < dataaddr + len) {
204199767fToomas Soome			switch (ELF_R_TYPE(rela->r_info)) {
205199767fToomas Soome			case R_PPC_RELATIVE:
206199767fToomas Soome				w = relbase + rela->r_addend;
207199767fToomas Soome				bcopy(&w, (u_char *)data + (relbase +
208199767fToomas Soome				      rela->r_offset - dataaddr), sizeof(w));
209199767fToomas Soome				break;
210199767fToomas Soome			default:
211199767fToomas Soome				printf("\nunhandled relocation type %u\n",
212199767fToomas Soome				       (u_int)ELF_R_TYPE(rela->r_info));
213199767fToomas Soome				return (EFTYPE);
214199767fToomas Soome			}
215199767fToomas Soome		}
216199767fToomas Soome		break;
217199767fToomas Soome	}
218199767fToomas Soome
219199767fToomas Soome	return (0);
220199767fToomas Soome#else
221199767fToomas Soome	(void)ef;
222199767fToomas Soome	(void)symaddr;
223199767fToomas Soome	(void)reldata;
224199767fToomas Soome	(void)reltype;
225199767fToomas Soome	(void)relbase;
226199767fToomas Soome	(void)dataaddr;
227199767fToomas Soome	(void)data;
228199767fToomas Soome	(void)len;
229199767fToomas Soome	return (EOPNOTSUPP);
230199767fToomas Soome#endif
231199767fToomas Soome}
232