git: 30700bc80e8e - releng/13.1 - linker: Simplify CTF container handling

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 18 Apr 2022 16:39:26 UTC
The branch releng/13.1 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=30700bc80e8e156672acec9f880b09825a1ebb05

commit 30700bc80e8e156672acec9f880b09825a1ebb05
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-03-07 13:43:42 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-04-18 16:38:14 +0000

    linker: Simplify CTF container handling
    
    Use sys/ctf.h to provide various definitions required to parse the CTF
    header.  No functional change intended.
    
    Approved by:    re (gjb)
    Reviewed by:    Domagoj Stolfa, emaste
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit cab9382a2cdf88b3027370cbdf73b095598eb7c1)
    (cherry picked from commit 24597a09b946ccaec4c7e43eae45c81b21d247a3)
---
 sys/kern/kern_ctf.c | 44 ++++++++++++++++----------------------------
 1 file changed, 16 insertions(+), 28 deletions(-)

diff --git a/sys/kern/kern_ctf.c b/sys/kern/kern_ctf.c
index ee7576ab6fb9..55e73b78c8ed 100644
--- a/sys/kern/kern_ctf.c
+++ b/sys/kern/kern_ctf.c
@@ -28,16 +28,11 @@
  * $FreeBSD$
  */
 
+#include <sys/ctf.h>
+
 /*
  * Note this file is included by both link_elf.c and link_elf_obj.c.
- *
- * The CTF header structure definition can't be used here because it's
- * (annoyingly) covered by the CDDL. We will just use a few bytes from
- * it as an integer array where we 'know' what they mean.
  */
-#define CTF_HDR_SIZE		36
-#define CTF_HDR_STRTAB_U32	7
-#define CTF_HDR_STRLEN_U32	8
 
 #ifdef DDB_CTF
 #include <contrib/zlib/zlib.h>
@@ -59,7 +54,7 @@ link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
 	size_t sz;
 	struct nameidata nd;
 	struct thread *td = curthread;
-	uint8_t ctf_hdr[CTF_HDR_SIZE];
+	struct ctf_header cth;
 #endif
 	int error = 0;
 
@@ -174,17 +169,13 @@ link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
 	}
 
 	/* Read the CTF header. */
-	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, ctf_hdr, sizeof(ctf_hdr),
+	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, &cth, sizeof(cth),
 	    shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
 	    NOCRED, NULL, td)) != 0)
 		goto out;
 
 	/* Check the CTF magic number. */
-#ifdef __LITTLE_ENDIAN__
-	if (ctf_hdr[0] != 0xf1 || ctf_hdr[1] != 0xcf) {
-#else
-	if (ctf_hdr[0] != 0xcf || ctf_hdr[1] != 0xf1) {
-#endif
+	if (cth.cth_magic != CTF_MAGIC) {
 		printf("%s(%d): module %s has invalid format\n",
 		    __func__, __LINE__, lf->pathname);
 		error = EFTYPE;
@@ -192,27 +183,24 @@ link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
 	}
 
 	/* Check if version 2. */
-	if (ctf_hdr[2] != 2) {
+	if (cth.cth_version != CTF_VERSION_2) {
 		printf("%s(%d): module %s CTF format version is %d "
 		    "(2 expected)\n",
-		    __func__, __LINE__, lf->pathname, ctf_hdr[2]);
+		    __func__, __LINE__, lf->pathname, cth.cth_version);
 		error = EFTYPE;
 		goto out;
 	}
 
 	/* Check if the data is compressed. */
-	if ((ctf_hdr[3] & 0x1) != 0) {
-		uint32_t *u32 = (uint32_t *) ctf_hdr;
-
+	if ((cth.cth_flags & CTF_F_COMPRESS) != 0) {
 		/*
 		 * The last two fields in the CTF header are the offset
 		 * from the end of the header to the start of the string
-		 * data and the length of that string data. se this
+		 * data and the length of that string data. Use this
 		 * information to determine the decompressed CTF data
 		 * buffer required.
 		 */
-		sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] +
-		    sizeof(ctf_hdr);
+		sz = cth.cth_stroff + cth.cth_strlen + sizeof(cth);
 
 		/*
 		 * Allocate memory for the compressed CTF data, including
@@ -251,14 +239,14 @@ link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
 		 * The header isn't compressed, so copy that into the
 		 * CTF buffer first.
 		 */
-		bcopy(ctf_hdr, ctftab, sizeof(ctf_hdr));
+		bcopy(&cth, ctftab, sizeof(cth));
 
-		destlen = sz - sizeof(ctf_hdr);
-		ret = uncompress(((uint8_t *) ctftab) + sizeof(ctf_hdr),
-		    &destlen, ((uint8_t *) raw) + sizeof(ctf_hdr),
-		    shdr[i].sh_size - sizeof(ctf_hdr));
+		destlen = sz - sizeof(cth);
+		ret = uncompress(ctftab + sizeof(cth), &destlen,
+		    raw + sizeof(cth), shdr[i].sh_size - sizeof(cth));
 		if (ret != Z_OK) {
-			printf("%s(%d): zlib uncompress returned %d\n", __func__, __LINE__, ret);
+			printf("%s(%d): zlib uncompress returned %d\n",
+			    __func__, __LINE__, ret);
 			error = EIO;
 			goto out;
 		}