xref: /freebsd/usr.sbin/kldxref/ef_mips.c (revision 2a622f14)
1c1bba444SJohn Baldwin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3c1bba444SJohn Baldwin  *
4c1bba444SJohn Baldwin  * Copyright (c) 2019 John Baldwin <jhb@FreeBSD.org>
5c1bba444SJohn Baldwin  *
6c1bba444SJohn Baldwin  * This software was developed by SRI International and the University of
7c1bba444SJohn Baldwin  * Cambridge Computer Laboratory (Department of Computer Science and
8c1bba444SJohn Baldwin  * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9c1bba444SJohn Baldwin  * DARPA SSITH research programme.
10c1bba444SJohn Baldwin  *
11c1bba444SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
12c1bba444SJohn Baldwin  * modification, are permitted provided that the following conditions
13c1bba444SJohn Baldwin  * are met:
14c1bba444SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
15c1bba444SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
16c1bba444SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
17c1bba444SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
18c1bba444SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
19c1bba444SJohn Baldwin  *
20c1bba444SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21c1bba444SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22c1bba444SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23c1bba444SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24c1bba444SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25c1bba444SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26c1bba444SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27c1bba444SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28c1bba444SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29c1bba444SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30c1bba444SJohn Baldwin  * SUCH DAMAGE.
31c1bba444SJohn Baldwin  */
32c1bba444SJohn Baldwin 
330299afdfSJohn Baldwin #include <sys/endian.h>
34c1bba444SJohn Baldwin 
35c1bba444SJohn Baldwin #include <err.h>
36c1bba444SJohn Baldwin #include <errno.h>
370299afdfSJohn Baldwin #include <gelf.h>
38c1bba444SJohn Baldwin 
39c1bba444SJohn Baldwin #include "ef.h"
40c1bba444SJohn Baldwin 
41c1bba444SJohn Baldwin /*
422a622f14SJessica Clarke  * Apply relocations to the values obtained from the file. `relbase' is the
432a622f14SJessica Clarke  * target relocation address of the section, and `dataoff/len' is the region
442a622f14SJessica Clarke  * that is to be relocated, and has been copied to *dest
45c1bba444SJohn Baldwin  */
460299afdfSJohn Baldwin static int
ef_mips_reloc(struct elf_file * ef,const void * reldata,Elf_Type reltype,GElf_Addr relbase,GElf_Addr dataoff,size_t len,void * dest)470299afdfSJohn Baldwin ef_mips_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype,
480299afdfSJohn Baldwin     GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest)
49c1bba444SJohn Baldwin {
500299afdfSJohn Baldwin 	char *where;
512a622f14SJessica Clarke 	GElf_Addr addr, addend;
522a622f14SJessica Clarke 	GElf_Size rtype, symidx;
530299afdfSJohn Baldwin 	const GElf_Rel *rel;
540299afdfSJohn Baldwin 	const GElf_Rela *rela;
55c1bba444SJohn Baldwin 
56c1bba444SJohn Baldwin 	switch (reltype) {
570299afdfSJohn Baldwin 	case ELF_T_REL:
580299afdfSJohn Baldwin 		rel = (const GElf_Rel *)reldata;
592a622f14SJessica Clarke 		where = (char *)dest + (relbase + rel->r_offset - dataoff);
60c1bba444SJohn Baldwin 		addend = 0;
610299afdfSJohn Baldwin 		rtype = GELF_R_TYPE(rel->r_info);
620299afdfSJohn Baldwin 		symidx = GELF_R_SYM(rel->r_info);
63c1bba444SJohn Baldwin 		break;
640299afdfSJohn Baldwin 	case ELF_T_RELA:
650299afdfSJohn Baldwin 		rela = (const GElf_Rela *)reldata;
662a622f14SJessica Clarke 		where = (char *)dest + (relbase + rela->r_offset - dataoff);
67c1bba444SJohn Baldwin 		addend = rela->r_addend;
680299afdfSJohn Baldwin 		rtype = GELF_R_TYPE(rela->r_info);
690299afdfSJohn Baldwin 		symidx = GELF_R_SYM(rela->r_info);
70c1bba444SJohn Baldwin 		break;
71c1bba444SJohn Baldwin 	default:
72c1bba444SJohn Baldwin 		return (EINVAL);
73c1bba444SJohn Baldwin 	}
74c1bba444SJohn Baldwin 
750299afdfSJohn Baldwin 	if (where < (char *)dest || where >= (char *)dest + len)
76c1bba444SJohn Baldwin 		return (0);
77c1bba444SJohn Baldwin 
780299afdfSJohn Baldwin 	if (reltype == ELF_T_REL) {
790299afdfSJohn Baldwin 		if (elf_class(ef) == ELFCLASS64) {
800299afdfSJohn Baldwin 			if (elf_encoding(ef) == ELFDATA2LSB)
810299afdfSJohn Baldwin 				addend = le64dec(where);
820299afdfSJohn Baldwin 			else
830299afdfSJohn Baldwin 				addend = be64dec(where);
840299afdfSJohn Baldwin 		} else {
850299afdfSJohn Baldwin 			if (elf_encoding(ef) == ELFDATA2LSB)
860299afdfSJohn Baldwin 				addend = le32dec(where);
870299afdfSJohn Baldwin 			else
880299afdfSJohn Baldwin 				addend = be32dec(where);
890299afdfSJohn Baldwin 		}
900299afdfSJohn Baldwin 	}
91c1bba444SJohn Baldwin 
92c1bba444SJohn Baldwin 	switch (rtype) {
93c1bba444SJohn Baldwin 	case R_MIPS_64:		/* S + A */
942a622f14SJessica Clarke 		addr = EF_SYMADDR(ef, symidx) + addend;
950299afdfSJohn Baldwin 		if (elf_encoding(ef) == ELFDATA2LSB)
962a622f14SJessica Clarke 			le64enc(where, addr);
970299afdfSJohn Baldwin 		else
982a622f14SJessica Clarke 			be64enc(where, addr);
990299afdfSJohn Baldwin 		break;
1000299afdfSJohn Baldwin 	case R_MIPS_32:		/* S + A */
1012a622f14SJessica Clarke 		addr = EF_SYMADDR(ef, symidx) + addend;
1020299afdfSJohn Baldwin 		if (elf_encoding(ef) == ELFDATA2LSB)
1032a622f14SJessica Clarke 			le32enc(where, addr);
1040299afdfSJohn Baldwin 		else
1052a622f14SJessica Clarke 			be32enc(where, addr);
106c1bba444SJohn Baldwin 		break;
107c1bba444SJohn Baldwin 	default:
108c1bba444SJohn Baldwin 		warnx("unhandled relocation type %d", (int)rtype);
109c1bba444SJohn Baldwin 	}
110c1bba444SJohn Baldwin 	return (0);
111c1bba444SJohn Baldwin }
1120299afdfSJohn Baldwin 
1130299afdfSJohn Baldwin ELF_RELOC(ELFCLASS32, ELFDATA2LSB, EM_MIPS, ef_mips_reloc);
1140299afdfSJohn Baldwin ELF_RELOC(ELFCLASS32, ELFDATA2MSB, EM_MIPS, ef_mips_reloc);
1150299afdfSJohn Baldwin ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_MIPS, ef_mips_reloc);
1160299afdfSJohn Baldwin ELF_RELOC(ELFCLASS64, ELFDATA2MSB, EM_MIPS, ef_mips_reloc);
117