xref: /freebsd/lib/libkldelf/ef_powerpc.c (revision 968bcca262a2ea8025924fe10e449bf89caf74bc)
1*968bcca2SKa Ho Ng /*-
2*968bcca2SKa Ho Ng  * SPDX-License-Identifier: BSD-2-Clause
3*968bcca2SKa Ho Ng  *
4*968bcca2SKa Ho Ng  * Copyright (c) 2005 Peter Grehan.
5*968bcca2SKa Ho Ng  * Copyright 1996-1998 John D. Polstra.
6*968bcca2SKa Ho Ng  * All rights reserved.
7*968bcca2SKa Ho Ng  *
8*968bcca2SKa Ho Ng  * Redistribution and use in source and binary forms, with or without
9*968bcca2SKa Ho Ng  * modification, are permitted provided that the following conditions
10*968bcca2SKa Ho Ng  * are met:
11*968bcca2SKa Ho Ng  * 1. Redistributions of source code must retain the above copyright
12*968bcca2SKa Ho Ng  *    notice, this list of conditions and the following disclaimer.
13*968bcca2SKa Ho Ng  * 2. Redistributions in binary form must reproduce the above copyright
14*968bcca2SKa Ho Ng  *    notice, this list of conditions and the following disclaimer in the
15*968bcca2SKa Ho Ng  *    documentation and/or other materials provided with the distribution.
16*968bcca2SKa Ho Ng  *
17*968bcca2SKa Ho Ng  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*968bcca2SKa Ho Ng  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*968bcca2SKa Ho Ng  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*968bcca2SKa Ho Ng  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*968bcca2SKa Ho Ng  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*968bcca2SKa Ho Ng  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*968bcca2SKa Ho Ng  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*968bcca2SKa Ho Ng  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*968bcca2SKa Ho Ng  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*968bcca2SKa Ho Ng  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*968bcca2SKa Ho Ng  * SUCH DAMAGE.
28*968bcca2SKa Ho Ng  */
29*968bcca2SKa Ho Ng 
30*968bcca2SKa Ho Ng #include <sys/endian.h>
31*968bcca2SKa Ho Ng 
32*968bcca2SKa Ho Ng #include <err.h>
33*968bcca2SKa Ho Ng #include <errno.h>
34*968bcca2SKa Ho Ng #include <gelf.h>
35*968bcca2SKa Ho Ng 
36*968bcca2SKa Ho Ng #include "kldelf.h"
37*968bcca2SKa Ho Ng 
38*968bcca2SKa Ho Ng /*
39*968bcca2SKa Ho Ng  * Apply relocations to the values obtained from the file. `relbase' is the
40*968bcca2SKa Ho Ng  * target relocation address of the section, and `dataoff/len' is the region
41*968bcca2SKa Ho Ng  * that is to be relocated, and has been copied to *dest
42*968bcca2SKa Ho Ng  */
43*968bcca2SKa Ho Ng static int
ef_ppc_reloc(struct elf_file * ef,const void * reldata,Elf_Type reltype,GElf_Addr relbase,GElf_Addr dataoff,size_t len,void * dest)44*968bcca2SKa Ho Ng ef_ppc_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype,
45*968bcca2SKa Ho Ng     GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest)
46*968bcca2SKa Ho Ng {
47*968bcca2SKa Ho Ng 	char *where;
48*968bcca2SKa Ho Ng 	GElf_Addr addr, addend;
49*968bcca2SKa Ho Ng 	GElf_Size rtype, symidx;
50*968bcca2SKa Ho Ng 	const GElf_Rela *rela;
51*968bcca2SKa Ho Ng 
52*968bcca2SKa Ho Ng 	switch (reltype) {
53*968bcca2SKa Ho Ng 	case ELF_T_RELA:
54*968bcca2SKa Ho Ng 		rela = (const GElf_Rela *)reldata;
55*968bcca2SKa Ho Ng 		where = (char *)dest + (relbase + rela->r_offset - dataoff);
56*968bcca2SKa Ho Ng 		addend = rela->r_addend;
57*968bcca2SKa Ho Ng 		rtype = GELF_R_TYPE(rela->r_info);
58*968bcca2SKa Ho Ng 		symidx = GELF_R_SYM(rela->r_info);
59*968bcca2SKa Ho Ng 		break;
60*968bcca2SKa Ho Ng 	default:
61*968bcca2SKa Ho Ng 		return (EINVAL);
62*968bcca2SKa Ho Ng 	}
63*968bcca2SKa Ho Ng 
64*968bcca2SKa Ho Ng 	if (where < (char *)dest || where >= (char *)dest + len)
65*968bcca2SKa Ho Ng 		return (0);
66*968bcca2SKa Ho Ng 
67*968bcca2SKa Ho Ng 	switch (rtype) {
68*968bcca2SKa Ho Ng 	case R_PPC_RELATIVE: /* word32|doubleword64 B + A */
69*968bcca2SKa Ho Ng 		addr = relbase + addend;
70*968bcca2SKa Ho Ng 		if (elf_class(ef) == ELFCLASS64) {
71*968bcca2SKa Ho Ng 			if (elf_encoding(ef) == ELFDATA2LSB)
72*968bcca2SKa Ho Ng 				le64enc(where, addr);
73*968bcca2SKa Ho Ng 			else
74*968bcca2SKa Ho Ng 				be64enc(where, addr);
75*968bcca2SKa Ho Ng 		} else
76*968bcca2SKa Ho Ng 			be32enc(where, addr);
77*968bcca2SKa Ho Ng 		break;
78*968bcca2SKa Ho Ng 	case R_PPC_ADDR32:	/* word32 S + A */
79*968bcca2SKa Ho Ng 		addr = EF_SYMADDR(ef, symidx) + addend;
80*968bcca2SKa Ho Ng 		be32enc(where, addr);
81*968bcca2SKa Ho Ng 		break;
82*968bcca2SKa Ho Ng 	case R_PPC64_ADDR64:	/* doubleword64 S + A */
83*968bcca2SKa Ho Ng 		addr = EF_SYMADDR(ef, symidx) + addend;
84*968bcca2SKa Ho Ng 		if (elf_encoding(ef) == ELFDATA2LSB)
85*968bcca2SKa Ho Ng 			le64enc(where, addr);
86*968bcca2SKa Ho Ng 		else
87*968bcca2SKa Ho Ng 			be64enc(where, addr);
88*968bcca2SKa Ho Ng 		break;
89*968bcca2SKa Ho Ng 	default:
90*968bcca2SKa Ho Ng 		warnx("unhandled relocation type %d", (int)rtype);
91*968bcca2SKa Ho Ng 	}
92*968bcca2SKa Ho Ng 	return (0);
93*968bcca2SKa Ho Ng }
94*968bcca2SKa Ho Ng 
95*968bcca2SKa Ho Ng ELF_RELOC(ELFCLASS32, ELFDATA2MSB, EM_PPC, ef_ppc_reloc);
96*968bcca2SKa Ho Ng ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_PPC64, ef_ppc_reloc);
97*968bcca2SKa Ho Ng ELF_RELOC(ELFCLASS64, ELFDATA2MSB, EM_PPC64, ef_ppc_reloc);
98