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