xref: /freebsd/usr.sbin/kldxref/ef_powerpc.c (revision f552d7ad)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 Peter Grehan.
5  * Copyright 1996-1998 John D. Polstra.
6  * All rights reserved.
7  *
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.
16  *
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.
28  */
29 
30 #include <sys/endian.h>
31 
32 #include <err.h>
33 #include <errno.h>
34 #include <gelf.h>
35 
36 #include "ef.h"
37 
38 /*
39  * Apply relocations to the values obtained from the file. `relbase' is the
40  * target relocation address of the section, and `dataoff/len' is the region
41  * that is to be relocated, and has been copied to *dest
42  */
43 static int
44 ef_ppc_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype,
45     GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest)
46 {
47 	char *where;
48 	GElf_Addr addr, addend;
49 	GElf_Size rtype, symidx;
50 	const GElf_Rela *rela;
51 
52 	switch (reltype) {
53 	case ELF_T_RELA:
54 		rela = (const GElf_Rela *)reldata;
55 		where = (char *)dest + (relbase + rela->r_offset - dataoff);
56 		addend = rela->r_addend;
57 		rtype = GELF_R_TYPE(rela->r_info);
58 		symidx = GELF_R_SYM(rela->r_info);
59 		break;
60 	default:
61 		return (EINVAL);
62 	}
63 
64 	if (where < (char *)dest || where >= (char *)dest + len)
65 		return (0);
66 
67 	switch (rtype) {
68 	case R_PPC_RELATIVE: /* word32|doubleword64 B + A */
69 		addr = relbase + addend;
70 		if (elf_class(ef) == ELFCLASS64) {
71 			if (elf_encoding(ef) == ELFDATA2LSB)
72 				le64enc(where, addr);
73 			else
74 				be64enc(where, addr);
75 		} else
76 			be32enc(where, addr);
77 		break;
78 	case R_PPC_ADDR32:	/* word32 S + A */
79 		addr = EF_SYMADDR(ef, symidx) + addend;
80 		be32enc(where, addr);
81 		break;
82 	case R_PPC64_ADDR64:	/* doubleword64 S + A */
83 		addr = EF_SYMADDR(ef, symidx) + addend;
84 		if (elf_encoding(ef) == ELFDATA2LSB)
85 			le64enc(where, addr);
86 		else
87 			be64enc(where, addr);
88 		break;
89 	default:
90 		warnx("unhandled relocation type %d", (int)rtype);
91 	}
92 	return (0);
93 }
94 
95 ELF_RELOC(ELFCLASS32, ELFDATA2MSB, EM_PPC, ef_ppc_reloc);
96 ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_PPC64, ef_ppc_reloc);
97 ELF_RELOC(ELFCLASS64, ELFDATA2MSB, EM_PPC64, ef_ppc_reloc);
98