xref: /freebsd/lib/libkldelf/ef_amd64.c (revision 13da1af1cd677b7901d3bf4b9dbe3290b94130d5)
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 "kldelf.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_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 addr, addend;
49 	GElf_Size rtype, symidx;
50 	const GElf_Rel *rel;
51 	const GElf_Rela *rela;
52 
53 	switch (reltype) {
54 	case ELF_T_REL:
55 		rel = (const GElf_Rel *)reldata;
56 		where = (char *)dest + (relbase + rel->r_offset - dataoff);
57 		addend = 0;
58 		rtype = GELF_R_TYPE(rel->r_info);
59 		symidx = GELF_R_SYM(rel->r_info);
60 		break;
61 	case ELF_T_RELA:
62 		rela = (const GElf_Rela *)reldata;
63 		where = (char *)dest + (relbase + rela->r_offset - dataoff);
64 		addend = rela->r_addend;
65 		rtype = GELF_R_TYPE(rela->r_info);
66 		symidx = GELF_R_SYM(rela->r_info);
67 		break;
68 	default:
69 		return (EINVAL);
70 	}
71 
72 	if (where < (char *)dest || where >= (char *)dest + len)
73 		return (0);
74 
75 	if (reltype == ELF_T_REL) {
76 		/* Addend is 32 bit on 32 bit relocs */
77 		switch (rtype) {
78 		case R_X86_64_PC32:
79 		case R_X86_64_32S:
80 			addend = le32dec(where);
81 			break;
82 		default:
83 			addend = le64dec(where);
84 			break;
85 		}
86 	}
87 
88 	switch (rtype) {
89 	case R_X86_64_NONE:	/* none */
90 		break;
91 	case R_X86_64_64:	/* S + A */
92 		addr = EF_SYMADDR(ef, symidx) + addend;
93 		le64enc(where, addr);
94 		break;
95 	case R_X86_64_32S:	/* S + A sign extend */
96 		addr = EF_SYMADDR(ef, symidx) + addend;
97 		le32enc(where, addr);
98 		break;
99 	case R_X86_64_GLOB_DAT:	/* S */
100 		addr = EF_SYMADDR(ef, symidx);
101 		le64enc(where, addr);
102 		break;
103 	case R_X86_64_RELATIVE:	/* B + A */
104 		addr = relbase + addend;
105 		le64enc(where, addr);
106 		break;
107 	default:
108 		warnx("unhandled relocation type %d", (int)rtype);
109 	}
110 	return (0);
111 }
112 
113 ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_X86_64, ef_amd64_reloc);
114