xref: /freebsd/usr.sbin/kldxref/ef_powerpc.c (revision d1ce87ae)
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 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 addend, val;
49 	GElf_Size rtype, symidx;
50 	const GElf_Rela *rela;
51 
52 	if (reltype != ELF_T_RELA)
53 		return (EINVAL);
54 
55 	rela = (const GElf_Rela *)reldata;
56 	where = (char *)dest - dataoff + rela->r_offset;
57 	addend = rela->r_addend;
58 	rtype = GELF_R_TYPE(rela->r_info);
59 	symidx = GELF_R_SYM(rela->r_info);
60 
61 	if (where < (char *)dest || where >= (char *)dest + len)
62 		return (0);
63 
64 	switch (rtype) {
65 	case R_PPC_RELATIVE: /* word32|doubleword64 B + A */
66 		val = relbase + addend;
67 		if (elf_class(ef) == ELFCLASS64) {
68 			if (elf_encoding(ef) == ELFDATA2LSB)
69 				le64enc(where, val);
70 			else
71 				be64enc(where, val);
72 		} else
73 			be32enc(where, val);
74 		break;
75 	case R_PPC_ADDR32:	/* word32 S + A */
76 		val = EF_SYMADDR(ef, symidx) + addend;
77 		be32enc(where, val);
78 		break;
79 	case R_PPC64_ADDR64:	/* doubleword64 S + A */
80 		val = EF_SYMADDR(ef, symidx) + addend;
81 		if (elf_encoding(ef) == ELFDATA2LSB)
82 			le64enc(where, val);
83 		else
84 			be64enc(where, val);
85 		break;
86 	default:
87 		warnx("unhandled relocation type %d", (int)rtype);
88 	}
89 	return (0);
90 }
91 
92 ELF_RELOC(ELFCLASS32, ELFDATA2MSB, EM_PPC, ef_ppc_reloc);
93 ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_PPC64, ef_ppc_reloc);
94 ELF_RELOC(ELFCLASS64, ELFDATA2MSB, EM_PPC64, ef_ppc_reloc);
95