]> git.ipfire.org Git - pakfire.git/commitdiff
elf: Begin scaffolding for an object that handles ELF files
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 16:22:53 +0000 (16:22 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 16:22:53 +0000 (16:22 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/elf.c [new file with mode: 0644]
src/libpakfire/include/pakfire/elf.h [new file with mode: 0644]

index 5d911e71009d4f111a4792b4fc18f45e848d302c..95682a363957a41e6a8d6d721865f7c05e4ff7ff 100644 (file)
@@ -199,6 +199,7 @@ libpakfire_la_SOURCES = \
        src/libpakfire/deps.c \
        src/libpakfire/digest.c \
        src/libpakfire/dist.c \
+       src/libpakfire/elf.c \
        src/libpakfire/env.c \
        src/libpakfire/fhs.c \
        src/libpakfire/file.c \
@@ -255,6 +256,7 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/deps.h \
        src/libpakfire/include/pakfire/digest.h \
        src/libpakfire/include/pakfire/dist.h \
+       src/libpakfire/include/pakfire/elf.h \
        src/libpakfire/include/pakfire/env.h \
        src/libpakfire/include/pakfire/fhs.h \
        src/libpakfire/include/pakfire/file.h \
diff --git a/src/libpakfire/elf.c b/src/libpakfire/elf.c
new file mode 100644 (file)
index 0000000..cb41c47
--- /dev/null
@@ -0,0 +1,124 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2025 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+
+#include <pakfire/ctx.h>
+#include <pakfire/elf.h>
+
+// libelf
+#include <gelf.h>
+#include <elfutils/libdwelf.h>
+
+struct pakfire_elf {
+       struct pakfire_ctx* ctx;
+       int nrefs;
+
+       // File Descriptor
+       int fd;
+
+       // ELF Object
+       Elf* elf;
+};
+
+static int pakfire_elf_init_libelf(struct pakfire_ctx* ctx) {
+       // Initialize libelf
+       if (elf_version(EV_CURRENT) == EV_NONE) {
+               ERROR(ctx, "Could not initialize libelf: %s\n", elf_errmsg(-1));
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static void pakfire_elf_free(struct pakfire_elf* self) {
+       if (self->elf)
+               elf_end(self->elf);
+       if (self->fd >= 0)
+               close(self->fd);
+       if (self->ctx)
+               pakfire_ctx_unref(self->ctx);
+       free(self);
+}
+
+int pakfire_elf_open(struct pakfire_elf** elf, struct pakfire_ctx* ctx, int fd) {
+       struct pakfire_elf* self = NULL;
+       int r;
+
+       // Require a valid file descriptor
+       if (fd < 0)
+               return -EBADF;
+
+       // Allocate a new object
+       self = calloc(1, sizeof(*self));
+       if (!self)
+               return -errno;
+
+       // Store a reference to the context
+       self->ctx = pakfire_ctx_ref(ctx);
+
+       // Initialize the reference counter
+       self->nrefs = 1;
+
+       // Store the file descriptor
+       self->fd = dup(fd);
+       if (self->fd < 0) {
+               ERROR(self->ctx, "Could not duplicate file descriptor: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Make sure libelf is initialized
+       r = pakfire_elf_init_libelf(self->ctx);
+       if (r < 0)
+               goto ERROR;
+
+       // Open the ELF file
+       self->elf = elf_begin(self->fd, ELF_C_READ, NULL);
+       if (!self->elf) {
+               ERROR(self->ctx, "Could not open ELF file: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       // Return the pointer
+       *elf = self;
+
+       return 0;
+
+ERROR:
+       pakfire_elf_free(self);
+
+       return r;
+}
+
+struct pakfire_elf* pakfire_elf_ref(struct pakfire_elf* self) {
+       self->nrefs++;
+
+       return self;
+}
+
+struct pakfire_elf* pakfire_elf_unref(struct pakfire_elf* self) {
+       if (--self->nrefs > 0)
+               return self;
+
+       pakfire_elf_free(self);
+       return NULL;
+}
diff --git a/src/libpakfire/include/pakfire/elf.h b/src/libpakfire/include/pakfire/elf.h
new file mode 100644 (file)
index 0000000..7d5a55e
--- /dev/null
@@ -0,0 +1,38 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2025 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_ELF_H
+#define PAKFIRE_ELF_H
+
+// This is all private stuff
+#ifdef PAKFIRE_PRIVATE
+
+struct pakfire_elf;
+
+#include <pakfire/ctx.h>
+
+int pakfire_elf_open(struct pakfire_elf** elf, struct pakfire_ctx* ctx, int fd);
+
+struct pakfire_elf* pakfire_elf_ref(struct pakfire_elf* self);
+struct pakfire_elf* pakfire_elf_unref(struct pakfire_elf* self);
+
+#endif /* PAKFIRE_PRIVATE */
+
+#endif /* PAKFIRE_ELF_H */