From nobody Thu Jan 18 22:25:46 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 4TGHQ30l38z578ND; Thu, 18 Jan 2024 22:25:47 +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 4TGHQ307DCz40gB; Thu, 18 Jan 2024 22:25:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705616747; 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=xRfl5cMXselEkNNOKedXhP7Ybz2rpKxirh7zFoeZ5Gg=; b=Lih67u/usets9H7GZ/bZfbSdzOMRxtxZxZc36iQQqLH2hUjn9854hPKCqHis5Ma+lVOoWP JgaI8oAnH1jemNjlnFk4iCWBRc2ADkZ1EI1cWA6ZzFyBsBbAUIHHkylI+c/KejqyQGtctf 5SLJkB8Z8P3jA0XxyVOMUYGRQIolsX9N5/jdHSoIhYpNPrEr15Oo9h/axDfbI6FEqPTbVO sexicGeoQ7rR+y/uqD2kxyTZg/lXNVdjz7c2D+NYyLhioOi9ZH5pzaIvR1Q2uEKoSqT/Q8 1F7OmIHOT1R4J3CtzZBByLaVijjnzsV7Xs+UL3H9e2oTkUFjxfk0YZZPvMIRhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705616747; 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=xRfl5cMXselEkNNOKedXhP7Ybz2rpKxirh7zFoeZ5Gg=; b=VkM+xLNXXG2SH3FtNdVqk6mVR3S2mZ0ZAFnxu6HORwRT8A/dR44LxiHQuNWZ7OpFID8h48 quLtPmcogy7ld8z0LJHC52TfKZJJgixAGcRwvnVrp1anyNLnozgVUYa9eWQYBXxqQBZk6k Dm05IuuU0+7JqSykl7Ji3BojQzXH3cUzQydrDUlWGoNM/32bkvWtISxFsjdGOSVXQKzexA E0LJScQMWGT8Yl0CpQotltDKUANUxR5qAjiO0+ROCbGjVsc6BJawx/kExKTVGmqXb2WhoX eeXf/L/8Kp9VeaYDaWhkKGFRfkRHlixnz+JJUuTYVxJ0CHfpji2MQIwZEu6IwA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705616747; a=rsa-sha256; cv=none; b=RUucBp4wCsHhN387KieRzkCaVKY6BZAs8rrDqPI6QAA5tQ1se3aj0AnojlvRRoQNBYjwA/ xnzFRPViyJGHYWr4CQ24oTffzXMTYr1GXWFe8lzxZWBU5u/dmKhf6K4zaNwC08sVeawEmv oVc0NTKF3VmWUXPZATi4h67jxIADGlmkR0Ghe3Ve+lYZpIND84unipSombeGAPuG9Rgwxa oCI6ZkiP7D8A5ln92qQugK4BL5dGn+Q+vDOsM9IibA+KhuF3xsAYZsdPoCH1NBWj72QgK5 MznuWCe+bJlL4pMuC+GnYFJgPPrHyEH5D32sxsDJqzyrwMp//5rh8M/e9CF9tw== 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 4TGHQ26HWBzQHr; Thu, 18 Jan 2024 22:25:46 +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 40IMPkef088879; Thu, 18 Jan 2024 22:25:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40IMPkOB088876; Thu, 18 Jan 2024 22:25:46 GMT (envelope-from git) Date: Thu, 18 Jan 2024 22:25:46 GMT Message-Id: <202401182225.40IMPkOB088876@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: e676886a9031 - stable/14 - kldxref: Refactor PNP entry parsing, no functional change 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: e676886a9031f1e60dde4745ff38ebb1b7c7e82e Auto-Submitted: auto-generated The branch stable/14 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=e676886a9031f1e60dde4745ff38ebb1b7c7e82e commit e676886a9031f1e60dde4745ff38ebb1b7c7e82e Author: John Baldwin AuthorDate: 2023-12-12 23:30:16 +0000 Commit: John Baldwin CommitDate: 2024-01-18 21:24:52 +0000 kldxref: Refactor PNP entry parsing, no functional change - Add a free_pnp_list to complement parse_pnp_list. Add freeing of 'new_desc' which was previously leaked. - Move body of loop that checked a single pnp list element against a table entry into a parse_pnp_entry function to reduce indentation and split parse_entry into a smaller function. - Similarly, split out a record_pnp_info function from parse_entry which builds the pnp_list and walks a table. Reviewed by: imp Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D42965 (cherry picked from commit c40fa3dc98d3cd8c39605c19c9cc08026d8e72c9) --- usr.sbin/kldxref/kldxref.c | 247 +++++++++++++++++++++++++-------------------- 1 file changed, 139 insertions(+), 108 deletions(-) diff --git a/usr.sbin/kldxref/kldxref.c b/usr.sbin/kldxref/kldxref.c index ae0d22181688..913b64ee2a16 100644 --- a/usr.sbin/kldxref/kldxref.c +++ b/usr.sbin/kldxref/kldxref.c @@ -237,6 +237,7 @@ parse_pnp_list(const char *desc, char **new_desc, pnp_list *list) size_t new_desc_size; FILE *fp; + TAILQ_INIT(list); walker = desc; ep = desc + strlen(desc); off = 0; @@ -379,6 +380,142 @@ err: errx(1, "Parse error of description string %s", desc); } +static void +free_pnp_list(char *new_desc, pnp_list *list) +{ + struct pnp_elt *elt, *elt_tmp; + + TAILQ_FOREACH_SAFE(elt, list, next, elt_tmp) { + TAILQ_REMOVE(list, elt, next); + free(elt); + } + free(new_desc); +} + +static void +parse_pnp_entry(struct elf_file *ef, struct pnp_elt *elt, const char *walker) +{ + uint8_t v1; + uint16_t v2; + uint32_t v4; + int value; + char buffer[1024]; + + if (elt->pe_kind == TYPE_W32) { + memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); + value = v4 & 0xffff; + record_int(value); + if (verbose > 1) + printf("W32:%#x", value); + value = (v4 >> 16) & 0xffff; + record_int(value); + if (verbose > 1) + printf(":%#x;", value); + } else if (elt->pe_kind & TYPE_INT) { + switch (elt->pe_kind & TYPE_SZ_MASK) { + case 1: + memcpy(&v1, walker + elt->pe_offset, sizeof(v1)); + if ((elt->pe_kind & TYPE_FLAGGED) && v1 == 0xff) + value = -1; + else + value = v1; + break; + case 2: + memcpy(&v2, walker + elt->pe_offset, sizeof(v2)); + if ((elt->pe_kind & TYPE_FLAGGED) && v2 == 0xffff) + value = -1; + else + value = v2; + break; + case 4: + memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); + if ((elt->pe_kind & TYPE_FLAGGED) && v4 == 0xffffffff) + value = -1; + else + value = v4; + break; + default: + errx(1, "Invalid size somehow %#x", elt->pe_kind); + } + if (verbose > 1) + printf("I:%#x;", value); + record_int(value); + } else if (elt->pe_kind == TYPE_T) { + /* Do nothing */ + } else { /* E, Z or D -- P already filtered */ + if (elt->pe_kind == TYPE_E) { + memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); + strcpy(buffer, pnp_eisaformat(v4)); + } else { + char *ptr; + + ptr = *(char **)(walker + elt->pe_offset); + buffer[0] = '\0'; + if (ptr != NULL) { + EF_SEG_READ_STRING(ef, (Elf_Off)ptr, + sizeof(buffer), buffer); + buffer[sizeof(buffer) - 1] = '\0'; + } + } + if (verbose > 1) + printf("%c:%s;", elt->pe_kind == TYPE_E ? 'E' : + (elt->pe_kind == TYPE_Z ? 'Z' : 'D'), buffer); + record_string(buffer); + } +} + +static void +record_pnp_info(struct elf_file *ef, const char *cval, + struct mod_pnp_match_info *pnp, const char *descr) +{ + pnp_list list; + struct pnp_elt *elt; + char *new_descr, *walker; + void *table; + size_t len; + int error, i; + + if (verbose > 1) + printf(" pnp info for bus %s format %s %d entries of %d bytes\n", + cval, descr, pnp->num_entry, pnp->entry_len); + + /* + * Parse descr to weed out the chaff and to create a list + * of offsets to output. + */ + parse_pnp_list(descr, &new_descr, &list); + record_int(MDT_PNP_INFO); + record_string(cval); + record_string(new_descr); + record_int(pnp->num_entry); + len = pnp->num_entry * pnp->entry_len; + table = malloc(len); + error = EF_SEG_READ_REL(ef, (Elf_Off)pnp->table, len, table); + if (error != 0) { + free_pnp_list(new_descr, &list); + free(table); + return; + } + + /* + * Walk the list and output things. We've collapsed all the + * variant forms of the table down to just ints and strings. + */ + walker = table; + for (i = 0; i < pnp->num_entry; i++) { + TAILQ_FOREACH(elt, &list, next) { + parse_pnp_entry(ef, elt, walker); + } + if (verbose > 1) + printf("\n"); + walker += pnp->entry_len; + } + + /* Now free it */ + free_pnp_list(new_descr, &list); + free(table); +} + static int parse_entry(struct mod_metadata *md, const char *cval, struct elf_file *ef, const char *kldname) @@ -388,10 +525,7 @@ parse_entry(struct mod_metadata *md, const char *cval, struct mod_pnp_match_info pnp; char descr[1024]; Elf_Off data; - int error, i; - size_t len; - char *walker; - void *table; + int error; data = (Elf_Off)md->md_data; error = 0; @@ -432,110 +566,7 @@ parse_entry(struct mod_metadata *md, const char *cval, printf(" pnp info for bus %s format %s %d entries of %d bytes\n", cval, descr, pnp.num_entry, pnp.entry_len); } else { - pnp_list list; - struct pnp_elt *elt, *elt_tmp; - char *new_descr; - - if (verbose > 1) - printf(" pnp info for bus %s format %s %d entries of %d bytes\n", - cval, descr, pnp.num_entry, pnp.entry_len); - /* - * Parse descr to weed out the chaff and to create a list - * of offsets to output. - */ - TAILQ_INIT(&list); - parse_pnp_list(descr, &new_descr, &list); - record_int(MDT_PNP_INFO); - record_string(cval); - record_string(new_descr); - record_int(pnp.num_entry); - len = pnp.num_entry * pnp.entry_len; - walker = table = malloc(len); - check(EF_SEG_READ_REL(ef, (Elf_Off)pnp.table, len, table)); - - /* - * Walk the list and output things. We've collapsed all the - * variant forms of the table down to just ints and strings. - */ - for (i = 0; i < pnp.num_entry; i++) { - TAILQ_FOREACH(elt, &list, next) { - uint8_t v1; - uint16_t v2; - uint32_t v4; - int value; - char buffer[1024]; - - if (elt->pe_kind == TYPE_W32) { - memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); - value = v4 & 0xffff; - record_int(value); - if (verbose > 1) - printf("W32:%#x", value); - value = (v4 >> 16) & 0xffff; - record_int(value); - if (verbose > 1) - printf(":%#x;", value); - } else if (elt->pe_kind & TYPE_INT) { - switch (elt->pe_kind & TYPE_SZ_MASK) { - case 1: - memcpy(&v1, walker + elt->pe_offset, sizeof(v1)); - if ((elt->pe_kind & TYPE_FLAGGED) && v1 == 0xff) - value = -1; - else - value = v1; - break; - case 2: - memcpy(&v2, walker + elt->pe_offset, sizeof(v2)); - if ((elt->pe_kind & TYPE_FLAGGED) && v2 == 0xffff) - value = -1; - else - value = v2; - break; - case 4: - memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); - if ((elt->pe_kind & TYPE_FLAGGED) && v4 == 0xffffffff) - value = -1; - else - value = v4; - break; - default: - errx(1, "Invalid size somehow %#x", elt->pe_kind); - } - if (verbose > 1) - printf("I:%#x;", value); - record_int(value); - } else if (elt->pe_kind == TYPE_T) { - /* Do nothing */ - } else { /* E, Z or D -- P already filtered */ - if (elt->pe_kind == TYPE_E) { - memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); - strcpy(buffer, pnp_eisaformat(v4)); - } else { - char *ptr; - - ptr = *(char **)(walker + elt->pe_offset); - buffer[0] = '\0'; - if (ptr != NULL) { - EF_SEG_READ_STRING(ef, (Elf_Off)ptr, - sizeof(buffer), buffer); - buffer[sizeof(buffer) - 1] = '\0'; - } - } - if (verbose > 1) - printf("%c:%s;", elt->pe_kind == TYPE_E ? 'E' : (elt->pe_kind == TYPE_Z ? 'Z' : 'D'), buffer); - record_string(buffer); - } - } - if (verbose > 1) - printf("\n"); - walker += pnp.entry_len; - } - /* Now free it */ - TAILQ_FOREACH_SAFE(elt, &list, next, elt_tmp) { - TAILQ_REMOVE(&list, elt, next); - free(elt); - } - free(table); + record_pnp_info(ef, cval, &pnp, descr); } break; default: