From nobody Thu Jan 18 22:25:56 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TGHQF2qTgz578bV; Thu, 18 Jan 2024 22:25:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TGHQF1Sftz40f4; Thu, 18 Jan 2024 22:25:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705616757; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Vj07kxgSZUIjO1Odxm5qOu/EBBr8/wg05r/MaJ3UXx4=; b=vGjSlFJ6c5MexGAZtFubOPcvhrj0eIRJfwwNme6hirhYOy2q9qLPuX82Hr5gx4o+LJugyw Yd47pm/gozoF6nmy9zn9C5C9YRq5KXFzwLumTvz37oriPZcogWdzMtlxneeibu0Ovu9PtS cdrp0soIgz+lLQnrreIqlG0nFtwC4Hv2qJ3jSZCqqxhvmMUSs3KH8x/zGkjgJ7PT5x0YU6 wv9UFbf/tLDWaXR3M6mcavF+1S23oljWz8fBlRHmCqtG4WAdz8OgjsxEpJ2IgbMZIzR42X RNEH13LE0nkonYbD000Dj5xGYEKU5KwxlzjYgWV2TrHgKJ+PoBLuEu/NiGN/BA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705616757; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Vj07kxgSZUIjO1Odxm5qOu/EBBr8/wg05r/MaJ3UXx4=; b=FneLg9m4pqJorRvd9F3UR25bf1PkKtcUigmIhKnJSv/rc0clzHDUqicGH8LQmi6WvU//JP J/leC1Nc2VsUBxx10E2pmgTyeCqiJ51ALNZnAxDFICP+PnS6lf5NMRNqKqj0F3gK6m6ZiX iGM0tOpcYuz4cHoEzooDYPsQUAF1Vp/PNOhBlgqfPFSST2Cmv000GY9g3sL2CcnpuLjv86 NFPETokrSov3qcvxGacaLesRNbfY880mwcxYmitOpOIWJXjemO1+Im/QNMf6yQ4F83yOWK XbOxQmnvrzQ4pZL24tLpO3qpuH8Y96yDcGmidkkh9l5/l25kwPE7xNZyNHW6cA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705616757; a=rsa-sha256; cv=none; b=kHNoe5AXSm0UB7ZMzTU4rr3Q1bByMEDpR57DfFl9c5RwLu08a0KftEr6udDyOa6W4OCehl S6yorhCZvYw7Mri6OSvZ5wUxA0lXJsghCc+NtEHN4xCbMJ9pDeLOdBVMK1XYOxhdP7p8ek 8AIBF1obnZD1lLkrBaKkbn/YxVD8e+5mH4ipijdTWw24KRUb+HzeTmF+LeyevegcQ2YIg+ 93mBNPGCyvgprb3JXn6RuHgvlS203z2Kv7Rk2VDH5GtAq9JQtIswG6Nnd4oOuymF8VtSk9 n24ArrU95x9yOgFr2dNs87rZ9gBkz6OhckXg7DiGE5l8MNjSdr3nvVWwdWbHhw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TGHQF0P2BzQG4; Thu, 18 Jan 2024 22:25:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 40IMPu2H089344; Thu, 18 Jan 2024 22:25:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40IMPulK089341; Thu, 18 Jan 2024 22:25:56 GMT (envelope-from git) Date: Thu, 18 Jan 2024 22:25:56 GMT Message-Id: <202401182225.40IMPulK089341@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: 7ea7498f6406 - stable/14 - kldxref: Reduce divergence between per-architecture files List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 7ea7498f6406bec488957e3ec23ec4c89fa3c436 Auto-Submitted: auto-generated The branch stable/14 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=7ea7498f6406bec488957e3ec23ec4c89fa3c436 commit 7ea7498f6406bec488957e3ec23ec4c89fa3c436 Author: Jessica Clarke AuthorDate: 2023-12-14 20:17:20 +0000 Commit: John Baldwin CommitDate: 2024-01-18 21:30:57 +0000 kldxref: Reduce divergence between per-architecture files Note that relbase is always 0 for DSOs so its omission for __KLD_SHARED architectures was not a bug in practice. Whilst here, also parenthesise the dest offset for where to avoid transiently creating an out-of-bounds pointer, which is UB (though even on CHERI architectures, where capability bounds compression can result in that creating invalid capabilities that will trap on dereference, optimisation will reassociate to the correct form in practice and thus work just fine). (cherry picked from commit 2a622f14e8a588de654847ae264cdc3616528c9d) --- usr.sbin/kldxref/ef_aarch64.c | 22 +++++++++++++--------- usr.sbin/kldxref/ef_amd64.c | 28 ++++++++++++---------------- usr.sbin/kldxref/ef_arm.c | 12 ++++++------ usr.sbin/kldxref/ef_i386.c | 17 ++++++++--------- usr.sbin/kldxref/ef_mips.c | 29 +++++++++++++---------------- usr.sbin/kldxref/ef_powerpc.c | 39 +++++++++++++++++++++------------------ usr.sbin/kldxref/ef_riscv.c | 15 ++++++++++----- 7 files changed, 83 insertions(+), 79 deletions(-) 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 @@ -47,21 +47,25 @@ ef_aarch64_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, 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; 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 @@ -36,17 +36,16 @@ #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; @@ -54,14 +53,14 @@ ef_amd64_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, 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); @@ -90,23 +89,20 @@ ef_amd64_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, 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); 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 @@ -37,9 +37,9 @@ #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, @@ -54,14 +54,14 @@ ef_arm_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, 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); @@ -82,7 +82,7 @@ ef_arm_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, le32enc(where, addr); break; case R_ARM_RELATIVE: /* B + A */ - addr = addend + relbase; + addr = relbase + addend; le32enc(where, addr); break; default: 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 @@ -36,9 +36,9 @@ #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, @@ -53,14 +53,14 @@ ef_i386_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, 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); @@ -76,13 +76,12 @@ ef_i386_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, 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 */ 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 @@ -39,32 +39,31 @@ #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); @@ -92,20 +91,18 @@ ef_mips_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, 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); 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 @@ -40,48 +40,51 @@ * 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); 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 @@ -38,19 +38,24 @@ #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); @@ -68,7 +73,7 @@ ef_riscv_reloc(struct elf_file *ef, const void *reldata, Elf_Type reltype, le64enc(where, addr); break; case R_RISCV_RELATIVE: /* B + A */ - addr = addend + relbase; + addr = relbase + addend; le64enc(where, addr); break; default: