From: Michael Tremer Date: Wed, 1 Jan 2025 16:22:53 +0000 (+0000) Subject: elf: Begin scaffolding for an object that handles ELF files X-Git-Tag: 0.9.30~614 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c2b5f0b00e303e5665482aba5e79dd925a00f9f4;p=pakfire.git elf: Begin scaffolding for an object that handles ELF files Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 5d911e710..95682a363 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 000000000..cb41c4718 --- /dev/null +++ b/src/libpakfire/elf.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#include +#include + +// libelf +#include +#include + +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 index 000000000..7d5a55e51 --- /dev/null +++ b/src/libpakfire/include/pakfire/elf.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_ELF_H +#define PAKFIRE_ELF_H + +// This is all private stuff +#ifdef PAKFIRE_PRIVATE + +struct pakfire_elf; + +#include + +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 */