diff mbox series

systemd: extract dependencies from .note.dlopen ELF segments

Message ID 20241119125136.2507561-1-ross.burton@arm.com
State New
Headers show
Series systemd: extract dependencies from .note.dlopen ELF segments | expand

Commit Message

Ross Burton Nov. 19, 2024, 12:51 p.m. UTC
First, this is likely not the final implementation, but a RFC and
prototype.

Some binaries don't dynamically link to libraries, but instead at runtime
dlopen() them. This means extra work for distributions as the dependencies
are not detected automatically, so libraries may be missing.

systemd is one such project which does this, and in an attempt to solve
the packaging problem it also embeds the names of the libraries that can
potentially be opened at runtime into ELF notes.  These can be read to
generate package dependencies.  For example:

packages/cortexa57-poky-linux/systemd/libsystemd-shared: RRECOMMENDS: added "libkmod (['>= 33']) libzstd (['>= 1.5.6'])"
packages/cortexa57-poky-linux/systemd/libsystemd: RRECOMMENDS: added "libzstd (['>= 1.5.6'])"

I expect this code to be changed before merging. Whilst systemd is the
main user of his approach right now, I expect to see it used in more
places in the future so there's a reasonably good argument to merge it
into the core shlibs code.  Also it currently manually extracts and
parses the data, whereas maybe we should incorporate pyelftools into
meta/lib/oe and use that to parse ELF files across all of OE.

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 meta/recipes-core/systemd/dlopen-deps.inc  | 73 ++++++++++++++++++++++
 meta/recipes-core/systemd/systemd_256.7.bb |  2 +
 2 files changed, 75 insertions(+)
 create mode 100644 meta/recipes-core/systemd/dlopen-deps.inc
diff mbox series

Patch

diff --git a/meta/recipes-core/systemd/dlopen-deps.inc b/meta/recipes-core/systemd/dlopen-deps.inc
new file mode 100644
index 00000000000..eaf6ca1f79a
--- /dev/null
+++ b/meta/recipes-core/systemd/dlopen-deps.inc
@@ -0,0 +1,73 @@ 
+PACKAGEFUNCS =+ "package_generate_dlopen_deps"
+
+python package_generate_dlopen_deps() {
+    # https://systemd.io/ELF_DLOPEN_METADATA/
+
+    import struct, json
+
+    def extract_segment(filename, segment):
+        """
+        Return the named segment from the ELF.
+        """
+        import tempfile, subprocess
+
+        with tempfile.NamedTemporaryFile() as f:
+            cmd = [d.getVar("OBJCOPY"), "--dump-section", f"{segment}={f.name}", filename]
+            subprocess.run(cmd, check=True)
+            return f.read()
+
+    def parse(buffer, is_little):
+        deps = []
+        offset = 0
+        while offset < len(buffer):
+            format = f"{'<' if is_little else '>'}iii"
+            name_size, desc_size, note_type = struct.unpack_from(format, buffer, offset)
+            offset += struct.calcsize(format)
+
+            format = f"{name_size}s0i{desc_size}s0i"
+            if note_type == 0x407c0c0a:
+                name_b, desc_b = struct.unpack_from(format, buffer, offset)
+                name = name_b.strip(b"\x00").decode("ascii")
+                if name == "FDO":
+                    desc = desc_b.strip(b"\x00").decode("utf-8")
+                    deps.append(*json.loads(desc))
+            offset += struct.calcsize(format)
+        return deps
+
+    dep_map = {
+        "required": "RDEPENDS",
+        "recommended": "RRECOMMENDS",
+        "suggested": "RSUGGESTS"
+    }
+
+    shlibs = oe.package.read_shlib_providers(d)
+
+    for pkg, files in pkgfiles.items():
+        # Skip -dbg packages as we won't need to generate dependencies for those
+        # but scanning can take time
+        if pkg.endswith("-dbg"):
+            continue
+
+        for f in files:
+            # Skip symlinks, just look for real libraries
+            if cpath.islink(f):
+                continue
+
+            if ".so." in f or f.endswith(".so"):
+                try:
+                    elf = oe.qa.ELFFile(f)
+                    elf.open()
+                    for dep in parse(extract_segment(f, ".note.dlopen"), elf.isLittleEndian()):
+                        for soname in dep["soname"]:
+                            if soname in shlibs:
+                                # TODO assumes the first match is good
+                                package, version = list(shlibs[soname].values())[0]
+                                dependency = dep_map[dep["priority"]]
+                                bb.note(f"{pkg}: adding {dependency} on {package} via .note.dlopen")
+                                d.appendVar(f"{dependency}:{pkg}", f" {package} (>= {version})")
+                            else:
+                                bb.warn(f"cannot find {soname}")
+                except oe.qa.NotELFFileError as e:
+                    bb.note(f"Cannot extract ELF notes: {e}")
+                    pass
+}
diff --git a/meta/recipes-core/systemd/systemd_256.7.bb b/meta/recipes-core/systemd/systemd_256.7.bb
index f3af4ac44dc..4942d9518aa 100644
--- a/meta/recipes-core/systemd/systemd_256.7.bb
+++ b/meta/recipes-core/systemd/systemd_256.7.bb
@@ -909,3 +909,5 @@  pkg_postinst:udev-hwdb () {
 pkg_prerm:udev-hwdb () {
 	rm -f $D${sysconfdir}/udev/hwdb.bin
 }
+
+require dlopen-deps.inc