diff --git a/usr.sbin/kldxref/ef_aarch64.c b/usr.sbin/kldxref/ef_aarch64.c index 4365a7ff1f66..b61de3b032ab 100644 --- a/usr.sbin/kldxref/ef_aarch64.c +++ b/usr.sbin/kldxref/ef_aarch64.c @@ -1,77 +1,81 @@ /*- * Copyright (c) 2005 Peter Grehan. * Copyright 1996-1998 John D. Polstra. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include "ef.h" /* * Apply relocations to the values obtained from the file. `relbase' is the * target relocation address of the section, and `dataoff/len' is the region * that is to be relocated, and has been copied to *dest */ 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) { char *where; GElf_Addr addr, addend; GElf_Size rtype, symidx; const GElf_Rela *rela; - if (reltype != ELF_T_RELA) + switch (reltype) { + case ELF_T_RELA: + rela = (const GElf_Rela *)reldata; + where = (char *)dest + (relbase + rela->r_offset - dataoff); + addend = rela->r_addend; + rtype = GELF_R_TYPE(rela->r_info); + symidx = GELF_R_SYM(rela->r_info); + break; + default: return (EINVAL); - - rela = (const GElf_Rela *)reldata; - where = (char *)dest - dataoff + rela->r_offset; - addend = rela->r_addend; - rtype = GELF_R_TYPE(rela->r_info); - symidx = GELF_R_SYM(rela->r_info); + } if (where < (char *)dest || where >= (char *)dest + len) return (0); - switch(rtype) { + switch (rtype) { case R_AARCH64_RELATIVE: - le64enc(where, relbase + addend); + addr = relbase + addend; + le64enc(where, addr); break; case R_AARCH64_ABS64: addr = EF_SYMADDR(ef, symidx) + addend; le64enc(where, addr); break; default: warnx("unhandled relocation type %d", (int)rtype); break; } return (0); } ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_AARCH64, ef_aarch64_reloc); diff --git a/usr.sbin/kldxref/ef_amd64.c b/usr.sbin/kldxref/ef_amd64.c index 729039daa509..fde032dcabc2 100644 --- a/usr.sbin/kldxref/ef_amd64.c +++ b/usr.sbin/kldxref/ef_amd64.c @@ -1,117 +1,113 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2003 Jake Burkholder. * Copyright 1996-1998 John D. Polstra. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include "ef.h" /* - * Apply relocations to the values we got from the file. `relbase' is the - * target relocation address of the section, and `dataoff' is the target - * relocation address of the data in `dest'. + * Apply relocations to the values obtained from the file. `relbase' is the + * target relocation address of the section, and `dataoff/len' is the region + * that is to be relocated, and has been copied to *dest */ static int ef_amd64_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest) { char *where; - GElf_Addr val; - GElf_Addr addend, addr; + GElf_Addr addr, addend; GElf_Size rtype, symidx; const GElf_Rel *rel; const GElf_Rela *rela; switch (reltype) { case ELF_T_REL: rel = (const GElf_Rel *)reldata; - where = (char *)dest + relbase + rel->r_offset - dataoff; + where = (char *)dest + (relbase + rel->r_offset - dataoff); addend = 0; rtype = GELF_R_TYPE(rel->r_info); symidx = GELF_R_SYM(rel->r_info); break; case ELF_T_RELA: rela = (const GElf_Rela *)reldata; - where = (char *)dest + relbase + rela->r_offset - dataoff; + where = (char *)dest + (relbase + rela->r_offset - dataoff); addend = rela->r_addend; rtype = GELF_R_TYPE(rela->r_info); symidx = GELF_R_SYM(rela->r_info); break; default: return (EINVAL); } if (where < (char *)dest || where >= (char *)dest + len) return (0); if (reltype == ELF_T_REL) { /* Addend is 32 bit on 32 bit relocs */ switch (rtype) { case R_X86_64_PC32: case R_X86_64_32S: addend = le32dec(where); break; default: addend = le64dec(where); break; } } switch (rtype) { case R_X86_64_NONE: /* none */ break; case R_X86_64_64: /* S + A */ - addr = EF_SYMADDR(ef, symidx); - val = addr + addend; - le64enc(where, val); + addr = EF_SYMADDR(ef, symidx) + addend; + le64enc(where, addr); break; case R_X86_64_32S: /* S + A sign extend */ - addr = EF_SYMADDR(ef, symidx); - val = (Elf32_Addr)(addr + addend); - le32enc(where, val); + addr = EF_SYMADDR(ef, symidx) + addend; + le32enc(where, addr); break; case R_X86_64_GLOB_DAT: /* S */ addr = EF_SYMADDR(ef, symidx); le64enc(where, addr); break; case R_X86_64_RELATIVE: /* B + A */ - addr = addend + relbase; - val = addr; - le64enc(where, val); + addr = relbase + addend; + le64enc(where, addr); break; default: warnx("unhandled relocation type %d", (int)rtype); } return (0); } ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_X86_64, ef_amd64_reloc); diff --git a/usr.sbin/kldxref/ef_arm.c b/usr.sbin/kldxref/ef_arm.c index 4b926839ee5d..cc5e265f821e 100644 --- a/usr.sbin/kldxref/ef_arm.c +++ b/usr.sbin/kldxref/ef_arm.c @@ -1,94 +1,94 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2003 Jake Burkholder. * Copyright 1996-1998 John D. Polstra. * All rights reserved. * Copyright (c) 2023 Jessica Clarke * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include "ef.h" /* - * Apply relocations to the values we got from the file. `relbase' is the - * target relocation address of the section, and `dataoff' is the target - * relocation address of the data in `dest'. + * Apply relocations to the values obtained from the file. `relbase' is the + * target relocation address of the section, and `dataoff/len' is the region + * that is to be relocated, and has been copied to *dest */ static int ef_arm_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest) { char *where; GElf_Addr addr, addend; GElf_Size rtype, symidx; const GElf_Rel *rel; const GElf_Rela *rela; switch (reltype) { case ELF_T_REL: rel = (const GElf_Rel *)reldata; - where = (char *)dest + relbase + rel->r_offset - dataoff; + where = (char *)dest + (relbase + rel->r_offset - dataoff); addend = 0; rtype = GELF_R_TYPE(rel->r_info); symidx = GELF_R_SYM(rel->r_info); break; case ELF_T_RELA: rela = (const GElf_Rela *)reldata; - where = (char *)dest + relbase + rela->r_offset - dataoff; + where = (char *)dest + (relbase + rela->r_offset - dataoff); addend = rela->r_addend; rtype = GELF_R_TYPE(rela->r_info); symidx = GELF_R_SYM(rela->r_info); break; default: return (EINVAL); } if (where < (char *)dest || where >= (char *)dest + len) return (0); if (reltype == ELF_T_REL) addend = le32dec(where); switch (rtype) { case R_ARM_ABS32: /* S + A */ addr = EF_SYMADDR(ef, symidx) + addend; le32enc(where, addr); break; case R_ARM_RELATIVE: /* B + A */ - addr = addend + relbase; + addr = relbase + addend; le32enc(where, addr); break; default: warnx("unhandled relocation type %d", (int)rtype); } return (0); } ELF_RELOC(ELFCLASS32, ELFDATA2LSB, EM_ARM, ef_arm_reloc); diff --git a/usr.sbin/kldxref/ef_i386.c b/usr.sbin/kldxref/ef_i386.c index e4f73877c430..962ed2bc0664 100644 --- a/usr.sbin/kldxref/ef_i386.c +++ b/usr.sbin/kldxref/ef_i386.c @@ -1,98 +1,97 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2003 Jake Burkholder. * Copyright 1996-1998 John D. Polstra. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include "ef.h" /* - * Apply relocations to the values we got from the file. `relbase' is the - * target relocation address of the section, and `dataoff' is the target - * relocation address of the data in `dest'. + * Apply relocations to the values obtained from the file. `relbase' is the + * target relocation address of the section, and `dataoff/len' is the region + * that is to be relocated, and has been copied to *dest */ static int ef_i386_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest) { char *where; GElf_Addr addr, addend; GElf_Size rtype, symidx; const GElf_Rel *rel; const GElf_Rela *rela; switch (reltype) { case ELF_T_REL: rel = (const GElf_Rel *)reldata; - where = (char *)dest + relbase + rel->r_offset - dataoff; + where = (char *)dest + (relbase + rel->r_offset - dataoff); addend = 0; rtype = GELF_R_TYPE(rel->r_info); symidx = GELF_R_SYM(rel->r_info); break; case ELF_T_RELA: rela = (const GElf_Rela *)reldata; - where = (char *)dest + relbase + rela->r_offset - dataoff; + where = (char *)dest + (relbase + rela->r_offset - dataoff); addend = rela->r_addend; rtype = GELF_R_TYPE(rela->r_info); symidx = GELF_R_SYM(rela->r_info); break; default: return (EINVAL); } if (where < (char *)dest || where >= (char *)dest + len) return (0); if (reltype == ELF_T_REL) addend = le32dec(where); switch (rtype) { - case R_386_RELATIVE: /* A + B */ - addr = addend + relbase; + case R_386_RELATIVE: /* B + A */ + addr = relbase + addend; le32enc(where, addr); break; case R_386_32: /* S + A - P */ - addr = EF_SYMADDR(ef, symidx); - addr += addend; + addr = EF_SYMADDR(ef, symidx) + addend; le32enc(where, addr); break; case R_386_GLOB_DAT: /* S */ addr = EF_SYMADDR(ef, symidx); le32enc(where, addr); break; default: warnx("unhandled relocation type %d", (int)rtype); } return (0); } ELF_RELOC(ELFCLASS32, ELFDATA2LSB, EM_386, ef_i386_reloc); diff --git a/usr.sbin/kldxref/ef_mips.c b/usr.sbin/kldxref/ef_mips.c index 60fd35390d7e..e4aeedb5c08b 100644 --- a/usr.sbin/kldxref/ef_mips.c +++ b/usr.sbin/kldxref/ef_mips.c @@ -1,119 +1,116 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2019 John Baldwin * * This software was developed by SRI International and the University of * Cambridge Computer Laboratory (Department of Computer Science and * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the * DARPA SSITH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include "ef.h" /* - * Apply relocations to the values we got from the file. `relbase' is the - * target relocation address of the section, and `dataoff' is the target - * relocation address of the data in `dest'. + * Apply relocations to the values obtained from the file. `relbase' is the + * target relocation address of the section, and `dataoff/len' is the region + * that is to be relocated, and has been copied to *dest */ 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) { char *where; - GElf_Addr val; + GElf_Addr addr, addend; + GElf_Size rtype, symidx; const GElf_Rel *rel; const GElf_Rela *rela; - GElf_Addr addend, addr; - GElf_Size rtype, symidx; switch (reltype) { case ELF_T_REL: rel = (const GElf_Rel *)reldata; - where = (char *)dest + relbase + rel->r_offset - dataoff; + where = (char *)dest + (relbase + rel->r_offset - dataoff); addend = 0; rtype = GELF_R_TYPE(rel->r_info); symidx = GELF_R_SYM(rel->r_info); break; case ELF_T_RELA: rela = (const GElf_Rela *)reldata; - where = (char *)dest + relbase + rela->r_offset - dataoff; + where = (char *)dest + (relbase + rela->r_offset - dataoff); addend = rela->r_addend; rtype = GELF_R_TYPE(rela->r_info); symidx = GELF_R_SYM(rela->r_info); break; default: return (EINVAL); } if (where < (char *)dest || where >= (char *)dest + len) return (0); if (reltype == ELF_T_REL) { if (elf_class(ef) == ELFCLASS64) { if (elf_encoding(ef) == ELFDATA2LSB) addend = le64dec(where); else addend = be64dec(where); } else { if (elf_encoding(ef) == ELFDATA2LSB) addend = le32dec(where); else addend = be32dec(where); } } switch (rtype) { case R_MIPS_64: /* S + A */ - addr = EF_SYMADDR(ef, symidx); - val = addr + addend; + addr = EF_SYMADDR(ef, symidx) + addend; if (elf_encoding(ef) == ELFDATA2LSB) - le64enc(where, val); + le64enc(where, addr); else - be64enc(where, val); + be64enc(where, addr); break; case R_MIPS_32: /* S + A */ - addr = EF_SYMADDR(ef, symidx); - val = addr + addend; + addr = EF_SYMADDR(ef, symidx) + addend; if (elf_encoding(ef) == ELFDATA2LSB) - le32enc(where, val); + le32enc(where, addr); else - be32enc(where, val); + be32enc(where, addr); break; default: warnx("unhandled relocation type %d", (int)rtype); } return (0); } ELF_RELOC(ELFCLASS32, ELFDATA2LSB, EM_MIPS, ef_mips_reloc); ELF_RELOC(ELFCLASS32, ELFDATA2MSB, EM_MIPS, ef_mips_reloc); ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_MIPS, ef_mips_reloc); ELF_RELOC(ELFCLASS64, ELFDATA2MSB, EM_MIPS, ef_mips_reloc); diff --git a/usr.sbin/kldxref/ef_powerpc.c b/usr.sbin/kldxref/ef_powerpc.c index ab33e170b488..f72cc1d85e20 100644 --- a/usr.sbin/kldxref/ef_powerpc.c +++ b/usr.sbin/kldxref/ef_powerpc.c @@ -1,94 +1,97 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2005 Peter Grehan. * Copyright 1996-1998 John D. Polstra. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include "ef.h" /* * Apply relocations to the values obtained from the file. `relbase' is the * target relocation address of the section, and `dataoff/len' is the region * that is to be relocated, and has been copied to *dest */ -int +static int ef_ppc_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest) { char *where; - GElf_Addr addend, val; + GElf_Addr addr, addend; GElf_Size rtype, symidx; const GElf_Rela *rela; - if (reltype != ELF_T_RELA) + switch (reltype) { + case ELF_T_RELA: + rela = (const GElf_Rela *)reldata; + where = (char *)dest + (relbase + rela->r_offset - dataoff); + addend = rela->r_addend; + rtype = GELF_R_TYPE(rela->r_info); + symidx = GELF_R_SYM(rela->r_info); + break; + default: return (EINVAL); - - rela = (const GElf_Rela *)reldata; - where = (char *)dest - dataoff + rela->r_offset; - addend = rela->r_addend; - rtype = GELF_R_TYPE(rela->r_info); - symidx = GELF_R_SYM(rela->r_info); + } if (where < (char *)dest || where >= (char *)dest + len) return (0); switch (rtype) { case R_PPC_RELATIVE: /* word32|doubleword64 B + A */ - val = relbase + addend; + addr = relbase + addend; if (elf_class(ef) == ELFCLASS64) { if (elf_encoding(ef) == ELFDATA2LSB) - le64enc(where, val); + le64enc(where, addr); else - be64enc(where, val); + be64enc(where, addr); } else - be32enc(where, val); + be32enc(where, addr); break; case R_PPC_ADDR32: /* word32 S + A */ - val = EF_SYMADDR(ef, symidx) + addend; - be32enc(where, val); + addr = EF_SYMADDR(ef, symidx) + addend; + be32enc(where, addr); break; case R_PPC64_ADDR64: /* doubleword64 S + A */ - val = EF_SYMADDR(ef, symidx) + addend; + addr = EF_SYMADDR(ef, symidx) + addend; if (elf_encoding(ef) == ELFDATA2LSB) - le64enc(where, val); + le64enc(where, addr); else - be64enc(where, val); + be64enc(where, addr); break; default: warnx("unhandled relocation type %d", (int)rtype); } return (0); } ELF_RELOC(ELFCLASS32, ELFDATA2MSB, EM_PPC, ef_ppc_reloc); ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_PPC64, ef_ppc_reloc); ELF_RELOC(ELFCLASS64, ELFDATA2MSB, EM_PPC64, ef_ppc_reloc); diff --git a/usr.sbin/kldxref/ef_riscv.c b/usr.sbin/kldxref/ef_riscv.c index 38299a1e9b46..46b9b66bee58 100644 --- a/usr.sbin/kldxref/ef_riscv.c +++ b/usr.sbin/kldxref/ef_riscv.c @@ -1,80 +1,85 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018 John Baldwin * * This software was developed by SRI International and the University of * Cambridge Computer Laboratory (Department of Computer Science and * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the * DARPA SSITH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include "ef.h" -int +/* + * Apply relocations to the values obtained from the file. `relbase' is the + * target relocation address of the section, and `dataoff/len' is the region + * that is to be relocated, and has been copied to *dest + */ +static int ef_riscv_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, GElf_Addr relbase, GElf_Addr dataoff, size_t len, void *dest) { char *where; - const GElf_Rela *rela; - GElf_Addr addend, addr; + GElf_Addr addr, addend; GElf_Size rtype, symidx; + const GElf_Rela *rela; switch (reltype) { case ELF_T_RELA: rela = (const GElf_Rela *)reldata; - where = (char *)dest + relbase + rela->r_offset - dataoff; + where = (char *)dest + (relbase + rela->r_offset - dataoff); addend = rela->r_addend; rtype = GELF_R_TYPE(rela->r_info); symidx = GELF_R_SYM(rela->r_info); break; default: return (EINVAL); } if (where < (char *)dest || where >= (char *)dest + len) return (0); switch (rtype) { case R_RISCV_64: /* S + A */ addr = EF_SYMADDR(ef, symidx) + addend; le64enc(where, addr); break; case R_RISCV_RELATIVE: /* B + A */ - addr = addend + relbase; + addr = relbase + addend; le64enc(where, addr); break; default: warnx("unhandled relocation type %d", (int)rtype); } return (0); } ELF_RELOC(ELFCLASS64, ELFDATA2LSB, EM_RISCV, ef_riscv_reloc);