git: 74051cfb3d18 - stable/15 - readelf: Add support for ELF package metadata note

Ed Maste <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a663764.1e12a.4e5e1bf0__22476.3460968219$1785083766$gmane$org@gitrepo.freebsd.org>
The branch stable/15 has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=74051cfb3d18bf6881afb8f19c3c0ccdfc2424ff

commit 74051cfb3d18bf6881afb8f19c3c0ccdfc2424ff
Author:     Ed Maste <[email protected]>
AuthorDate: 2022-04-20 14:12:06 +0000
Commit:     Ed Maste <[email protected]>
CommitDate: 2026-07-26 16:35:27 +0000

    readelf: Add support for ELF package metadata note
    
    We don't use this note type today, but as a general purpose ELF
    diagnostic tool readelf(1) ought to decode it.
    
    References:
    https://fedoraproject.org/wiki/Changes/Package_information_on_ELF_objects
    https://systemd.io/ELF_PACKAGE_METADATA/
    
    Reviewed by:    fuz
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D47524
    
    (cherry picked from commit c18512056301fa97dd31c4cc9a78e18f1aa8b2d5)
---
 contrib/elftoolchain/readelf/readelf.c | 38 +++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/contrib/elftoolchain/readelf/readelf.c b/contrib/elftoolchain/readelf/readelf.c
index daaa7bf62dff..3f0b9bfaa887 100644
--- a/contrib/elftoolchain/readelf/readelf.c
+++ b/contrib/elftoolchain/readelf/readelf.c
@@ -47,6 +47,7 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <vis.h>
 #include <zlib.h>
 
 #include <libcasper.h>
@@ -371,6 +372,7 @@ static int loc_at_comparator(const void *la1, const void *la2);
 static const char *mips_abi_fp(uint64_t fp);
 static const char *note_type(const char *note_name, unsigned int et,
     unsigned int nt);
+static const char *note_type_fdo(unsigned int nt);
 static const char *note_type_freebsd(unsigned int nt);
 static const char *note_type_freebsd_core(unsigned int nt);
 static const char *note_type_go(unsigned int nt);
@@ -1150,6 +1152,8 @@ note_type(const char *name, unsigned int et, unsigned int nt)
 			return note_type_freebsd_core(nt);
 		else
 			return note_type_freebsd(nt);
+	else if (strcmp(name, "FDO") == 0 && et != ET_CORE)
+		return note_type_fdo(nt);
 	else if (strcmp(name, "GNU") == 0 && et != ET_CORE)
 		return note_type_gnu(nt);
 	else if (strcmp(name, "Go") == 0 && et != ET_CORE)
@@ -1163,6 +1167,15 @@ note_type(const char *name, unsigned int et, unsigned int nt)
 	return note_type_unknown(nt);
 }
 
+static const char *
+note_type_fdo(unsigned int nt)
+{
+	switch (nt) {
+	case NT_FDO_PACKAGING_METADATA: return "NT_FDO_PACKAGING_METADATA";
+	default: return (note_type_unknown(nt));
+	}
+}
+
 static const char *
 note_type_freebsd(unsigned int nt)
 {
@@ -3819,7 +3832,30 @@ dump_notes_data(struct readelf *re, const char *name, uint32_t type,
 	}
 	ubuf = (const uint32_t *)(const void *)buf;
 
-	if (strcmp(name, "FreeBSD") == 0) {
+	if (strcmp(name, "FDO") == 0) {
+		switch (type) {
+		case NT_FDO_PACKAGING_METADATA:
+		{
+			char *visbuf;
+
+			if (sz > SIZE_MAX / 4 - 1) {
+				warnx("invalid note data");
+				return;
+			}
+			/* Ignore trailing NULs. */
+			while (sz > 0 && buf[sz - 1] == '\0') {
+				sz--;
+			}
+			if ((visbuf = malloc(sz * 4 + 1)) == NULL) {
+				err(1, "malloc");
+			}
+			(void)strvisx(visbuf, buf, sz, VIS_CSTYLE);
+			printf("   Package metadata: %s\n", visbuf);
+			free(visbuf);
+			return;
+		}
+		}
+	} else if (strcmp(name, "FreeBSD") == 0) {
 		switch (type) {
 		case NT_FREEBSD_ABI_TAG:
 			if (sz != 4)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.