]> git.ipfire.org Git - pakfire.git/commitdiff
ELF: Keep a simple reference to the ELF header at all times
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 16:37:10 +0000 (16:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Jan 2025 16:37:10 +0000 (16:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/elf.c
src/libpakfire/include/pakfire/elf.h

index 1f58f9af9f0546039d85ec3d2d0ef33464a76f46..5ff46bb2a9636536edcf5363d5242287f5b6faad 100644 (file)
@@ -41,6 +41,9 @@ struct pakfire_elf {
 
        // ELF Object
        Elf* elf;
+
+       // ELF Header
+       GElf_Ehdr ehdr;
 };
 
 static int pakfire_elf_init_libelf(struct pakfire_ctx* ctx) {
@@ -53,6 +56,42 @@ static int pakfire_elf_init_libelf(struct pakfire_ctx* ctx) {
        return 0;
 }
 
+static int pakfire_elf_open_elf(struct pakfire_elf* self) {
+       GElf_Ehdr* ehdr = NULL;
+       int r;
+
+       // Make sure libelf is initialized
+       r = pakfire_elf_init_libelf(self->ctx);
+       if (r < 0)
+               return r;
+
+       // 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");
+               return -errno;
+       }
+
+       // Is this actually an ELF file?
+       switch (elf_kind(self->elf)) {
+               case ELF_K_ELF:
+                       break;
+
+               // Fail for everything else
+               default:
+                       return -ENOTSUP;
+       }
+
+       // Fetch the ELF header
+       ehdr = gelf_getehdr(self->elf, &self->ehdr);
+       if (!ehdr) {
+               ERROR(self->ctx, "Could not fetch the ELF header: %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);
@@ -96,18 +135,10 @@ int pakfire_elf_open(struct pakfire_elf** elf,
                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;
+       r = pakfire_elf_open_elf(self);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Return the pointer
        *elf = self;
@@ -137,3 +168,7 @@ struct pakfire_elf* pakfire_elf_unref(struct pakfire_elf* self) {
 const char* pakfire_elf_path(struct pakfire_elf* self) {
        return self->path;
 }
+
+int pakfire_elf_type(struct pakfire_elf* self) {
+       return self->ehdr.e_type;
+}
index 7a04a6f25083b5de89f80f27a96ca7bc86387950..465d9362054e7b528b5dd372d2584bea14a8b994 100644 (file)
@@ -35,6 +35,7 @@ struct pakfire_elf* pakfire_elf_ref(struct pakfire_elf* self);
 struct pakfire_elf* pakfire_elf_unref(struct pakfire_elf* self);
 
 const char* pakfire_elf_path(struct pakfire_elf* self);
+int pakfire_elf_type(struct pakfire_elf* self);
 
 #endif /* PAKFIRE_PRIVATE */