xref: /freebsd/usr.sbin/kldxref/ef_amd64.c (revision 0299afdf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Jake Burkholder.
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 we got from the file. `relbase' is the
40  * target relocation address of the section, and `dataoff' is the target
41  * relocation address of the data in `dest'.
42  */
43 static int
44 ef_amd64_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 val;
49 	GElf_Addr addend, addr;
50 	GElf_Size rtype, symidx;
51 	const GElf_Rel *rel;
52 	const GElf_Rela *rela;
53 
54 	switch (reltype) {
55 	case ELF_T_REL:
56 		rel = (const GElf_Rel *)reldata;
57 		where = (char *)dest + relbase + rel->r_offset - dataoff;
58 		addend = 0;
59 		rtype = GELF_R_TYPE(rel->r_info);
60 		symidx = GELF_R_SYM(rel->r_info);
61 		break;
62 	case ELF_T_RELA:
63 		rela = (const GElf_Rela *)reldata;
64 		where = (char *)dest + relbase + rela->r_offset - dataoff;
65 		addend = rela->r_addend;
66 		rtype = GELF_R_TYPE(rela->r_info);
67 		symidx = GELF_R_SYM(rela->r_info);
68 		break;
69 	default:
70 		return (EINVAL);
71 	}
72 
73 	if (where < (char *)dest || where >= (char *)dest + len)
74 		return (0);
75 
76 	if (reltype == ELF_T_REL) {
77 		/* Addend is 32 bit on 32 bit relocs */
78 		switch (rtype) {
79 		case R_X86_64_PC32:
80 		case R_X86_64_32S:
81 			addend = le32dec(where);
82 			break;
83 		default:
84 			addend = le64dec(where);
85 			break;
86 		}
87 	}
88 
89 	switch (rtype) {
90 	case R_X86_64_NONE:	/* none */
91 		break;
92 	case R_X86_64_64:	/* S + A */
93 		addr = EF_SYMADDR(ef, symidx);
94 		val = addr + addend;
95 		le64enc(where, val);
96 		break;
97 	case R_X86_64_32S:	/* S + A sign extend */
98 		addr = EF_SYMADDR(ef, symidx);
99 		val = (Elf32_Addr)(addr + addend);
100 		le32enc(where, val);
101 		break;
102 	case R_X86_64_GLOB_DAT:	/* S */
103 		addr = EF_SYMADDR(ef, symidx);
104 		le64enc(where, addr);
105 		break;
106 	case R_X86_64_RELATIVE:	/* B + A */
107 		addr = addend + relbase;
108 		val = addr;
109 		le64enc(where, val);
110 		break;
111 	default:
112 		warnx("unhandled relocation type %d", (int)rtype);
113 	}
114 	return (0);
115 }
116 
117 ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_X86_64, ef_amd64_reloc);
118