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