--- /dev/null
+/*#############################################################################
+# #
+# 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;
+}
--- /dev/null
+/*#############################################################################
+# #
+# 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 */