]> git.ipfire.org Git - pakfire.git/commitdiff
elf: Create its own type
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Jun 2025 10:46:12 +0000 (10:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Jun 2025 10:46:12 +0000 (10:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/build.c
src/pakfire/elf.c
src/pakfire/elf.h
src/pakfire/file.c
src/pakfire/linter-file.c
src/pakfire/stripper.c

index 0027f2f94aa81efcc6579282d63db5cb3e93fb62..98bf25de76e628e8e050f1f7ba6c1c9522736c2b 100644 (file)
@@ -495,7 +495,7 @@ ERROR:
 
 static int pakfire_build_find_elf_provides(
                pakfire_ctx* ctx, struct pakfire_file* file, struct pakfire_find_deps_ctx* deps) {
-       struct pakfire_elf* elf = NULL;
+       pakfire_elf* elf = NULL;
        char** provides = NULL;
        int r;
 
@@ -546,7 +546,7 @@ ERROR:
 
 static int pakfire_build_find_elf_requires(
                pakfire_ctx* ctx, struct pakfire_file* file, struct pakfire_find_deps_ctx* deps) {
-       struct pakfire_elf* elf = NULL;
+       pakfire_elf* elf = NULL;
        char** requires = NULL;
        int r;
 
index 97540f5f29ff1f44dd006fcdd51082c0c860177b..9180c9665a9e9749a3edf26e492d2ea494a7036f 100644 (file)
@@ -78,7 +78,7 @@ static int pakfire_elf_init_libelf(pakfire_ctx* ctx) {
        return 0;
 }
 
-static int pakfire_elf_open_elf(struct pakfire_elf* self) {
+static int pakfire_elf_open_elf(pakfire_elf* self) {
        GElf_Ehdr* ehdr = NULL;
        int r;
 
@@ -129,7 +129,7 @@ static int pakfire_elf_open_elf(struct pakfire_elf* self) {
        return 0;
 }
 
-static void pakfire_elf_free(struct pakfire_elf* self) {
+static void pakfire_elf_free(pakfire_elf* self) {
        if (self->build_id)
                free(self->build_id);
        if (self->elf)
@@ -141,9 +141,9 @@ static void pakfire_elf_free(struct pakfire_elf* self) {
        free(self);
 }
 
-int pakfire_elf_open(struct pakfire_elf** elf,
+int pakfire_elf_open(pakfire_elf** elf,
                pakfire_ctx* ctx, const char* path, int fd) {
-       struct pakfire_elf* self = NULL;
+       pakfire_elf* self = NULL;
        int r;
 
        // Require a valid file descriptor
@@ -190,7 +190,7 @@ ERROR:
        return r;
 }
 
-int pakfire_elf_open_file(struct pakfire_elf** elf,
+int pakfire_elf_open_file(pakfire_elf** elf,
                pakfire_ctx* ctx, struct pakfire_file* file) {
        const char* path = NULL;
        int fd = -EBADF;
@@ -216,13 +216,13 @@ ERROR:
        return r;
 }
 
-struct pakfire_elf* pakfire_elf_ref(struct pakfire_elf* self) {
+pakfire_elf* pakfire_elf_ref(pakfire_elf* self) {
        self->nrefs++;
 
        return self;
 }
 
-struct pakfire_elf* pakfire_elf_unref(struct pakfire_elf* self) {
+pakfire_elf* pakfire_elf_unref(pakfire_elf* self) {
        if (--self->nrefs > 0)
                return self;
 
@@ -230,27 +230,27 @@ struct pakfire_elf* pakfire_elf_unref(struct pakfire_elf* self) {
        return NULL;
 }
 
-const char* pakfire_elf_path(struct pakfire_elf* self) {
+const char* pakfire_elf_path(pakfire_elf* self) {
        return self->path;
 }
 
-int pakfire_elf_type(struct pakfire_elf* self) {
+int pakfire_elf_type(pakfire_elf* self) {
        return self->ehdr.e_type;
 }
 
-int pakfire_elf_machine(struct pakfire_elf* self) {
+int pakfire_elf_machine(pakfire_elf* self) {
        return self->ehdr.e_machine;
 }
 
-int pakfire_elf_is_elf64(struct pakfire_elf* self) {
+int pakfire_elf_is_elf64(pakfire_elf* self) {
        return self->ehdr.e_ident[EI_CLASS] = ELFCLASS64;
 }
 
-int pakfire_elf_endianess(struct pakfire_elf* self) {
+int pakfire_elf_endianess(pakfire_elf* self) {
        return self->ehdr.e_ident[EI_DATA];
 }
 
-const char* pakfire_elf_build_id(struct pakfire_elf* self) {
+const char* pakfire_elf_build_id(pakfire_elf* self) {
        const void* buffer = NULL;
        ssize_t length;
 
@@ -280,7 +280,7 @@ const char* pakfire_elf_build_id(struct pakfire_elf* self) {
        return self->build_id;
 }
 
-const char* pakfire_elf_debuglink(struct pakfire_elf* self) {
+const char* pakfire_elf_debuglink(pakfire_elf* self) {
        GElf_Word crc32;
 
        // Fetch the debug link
@@ -297,9 +297,9 @@ const char* pakfire_elf_debuglink(struct pakfire_elf* self) {
 }
 
 typedef int (*pakfire_elf_foreach_program_header_callback)
-       (struct pakfire_elf* self, const GElf_Phdr* phdr, void* data);
+       (pakfire_elf* self, const GElf_Phdr* phdr, void* data);
 
-static int pakfire_elf_foreach_program_header(struct pakfire_elf* self,
+static int pakfire_elf_foreach_program_header(pakfire_elf* self,
                pakfire_elf_foreach_program_header_callback callback, void* data) {
        GElf_Phdr phdr = {};
        int r = 0;
@@ -320,7 +320,7 @@ static int pakfire_elf_foreach_program_header(struct pakfire_elf* self,
        return r;
 }
 
-static int pakfire_elf_get_section(struct pakfire_elf* self,
+static int pakfire_elf_get_section(pakfire_elf* self,
                const Elf64_Word type, const char* name, Elf_Scn** section, GElf_Shdr* header, Elf_Data** data) {
        const char* sname = NULL;
        Elf_Scn* s = NULL;
@@ -367,10 +367,10 @@ static int pakfire_elf_get_section(struct pakfire_elf* self,
        return 1;
 }
 
-typedef int (*pakfire_elf_foreach_section_callback)(struct pakfire_elf* self,
+typedef int (*pakfire_elf_foreach_section_callback)(pakfire_elf* self,
        Elf_Scn* section, const GElf_Shdr* shdr, void* data);
 
-static int pakfire_elf_foreach_section(struct pakfire_elf* self,
+static int pakfire_elf_foreach_section(pakfire_elf* self,
                const Elf64_Word type, pakfire_elf_foreach_section_callback callback, void* data) {
        Elf_Scn* section = NULL;
        GElf_Shdr shdr = {};
@@ -403,9 +403,9 @@ static int pakfire_elf_foreach_section(struct pakfire_elf* self,
 }
 
 typedef int (*pakfire_elf_dyn_walk_callback)
-       (struct pakfire_elf* self, const GElf_Shdr* shdr, const GElf_Dyn* dyn, void* data);
+       (pakfire_elf* self, const GElf_Shdr* shdr, const GElf_Dyn* dyn, void* data);
 
-static int pakfire_elf_dyn_walk(struct pakfire_elf* self,
+static int pakfire_elf_dyn_walk(pakfire_elf* self,
                pakfire_elf_dyn_walk_callback callback, void* data) {
        Elf_Scn* dynamic = NULL;
        GElf_Shdr shdr;
@@ -436,7 +436,7 @@ static int pakfire_elf_dyn_walk(struct pakfire_elf* self,
 }
 
 static int pakfire_elf_fetch_interpreter(
-               struct pakfire_elf* self, const GElf_Phdr* phdr, void* data) {
+               pakfire_elf* self, const GElf_Phdr* phdr, void* data) {
        Elf_Data* chunk = NULL;
 
        switch (phdr->p_type) {
@@ -458,7 +458,7 @@ static int pakfire_elf_fetch_interpreter(
        return 0;
 }
 
-const char* pakfire_elf_interpreter(struct pakfire_elf* self) {
+const char* pakfire_elf_interpreter(pakfire_elf* self) {
        int r;
 
        // Fetch the interpreter if not available
@@ -471,7 +471,7 @@ const char* pakfire_elf_interpreter(struct pakfire_elf* self) {
        return self->interpreter;
 }
 
-static int pakfire_elf_fetch_soname(struct pakfire_elf* self,
+static int pakfire_elf_fetch_soname(pakfire_elf* self,
                const GElf_Shdr* shdr, const GElf_Dyn* dyn, void* data) {
        switch (dyn->d_tag) {
                case DT_SONAME:
@@ -483,7 +483,7 @@ static int pakfire_elf_fetch_soname(struct pakfire_elf* self,
        return 0;
 }
 
-const char* pakfire_elf_soname(struct pakfire_elf* self) {
+const char* pakfire_elf_soname(pakfire_elf* self) {
        int r;
 
        // Fetch the SONAME if not available
@@ -496,7 +496,7 @@ const char* pakfire_elf_soname(struct pakfire_elf* self) {
        return self->soname;
 }
 
-int pakfire_elf_is_pie(struct pakfire_elf* self) {
+int pakfire_elf_is_pie(pakfire_elf* self) {
        switch (pakfire_elf_type(self)) {
                // Shared Object files are good
                case ET_DYN:
@@ -510,7 +510,7 @@ int pakfire_elf_is_pie(struct pakfire_elf* self) {
        return 0;
 }
 
-int pakfire_elf_has_ssp(struct pakfire_elf* self) {
+int pakfire_elf_has_ssp(pakfire_elf* self) {
        GElf_Sym symbol = {};
        int r;
 
@@ -580,7 +580,7 @@ int pakfire_elf_has_ssp(struct pakfire_elf* self) {
 }
 
 static int pakfire_elf_check_execstack(
-               struct pakfire_elf* self, const GElf_Phdr* phdr, void* data) {
+               pakfire_elf* self, const GElf_Phdr* phdr, void* data) {
        switch (phdr->p_type) {
                case PT_GNU_STACK:
                        DEBUG(self->ctx,
@@ -602,11 +602,11 @@ static int pakfire_elf_check_execstack(
        return 0;
 }
 
-int pakfire_elf_has_execstack(struct pakfire_elf* self) {
+int pakfire_elf_has_execstack(pakfire_elf* self) {
        return pakfire_elf_foreach_program_header(self, pakfire_elf_check_execstack, NULL);
 }
 
-static int pakfire_elf_has_bind_now(struct pakfire_elf* self,
+static int pakfire_elf_has_bind_now(pakfire_elf* self,
                const GElf_Shdr* shdr, const GElf_Dyn* dyn, void* data) {
        switch (dyn->d_tag) {
                case DT_BIND_NOW:
@@ -630,11 +630,11 @@ static int pakfire_elf_has_bind_now(struct pakfire_elf* self,
        return 0;
 }
 
-int pakfire_elf_is_fully_relro(struct pakfire_elf* self) {
+int pakfire_elf_is_fully_relro(pakfire_elf* self) {
        return pakfire_elf_dyn_walk(self, pakfire_elf_has_bind_now, NULL);
 }
 
-int pakfire_elf_is_partially_relro(struct pakfire_elf* self) {
+int pakfire_elf_is_partially_relro(pakfire_elf* self) {
        GElf_Phdr phdr;
 
        // Walk through all program headers
@@ -678,7 +678,7 @@ static uint32_t read_4_bytes(const int endianess, const char* data) {
        return bytes;
 }
 
-static int pakfire_elf_check_cf_protection_aarch64(struct pakfire_elf* self,
+static int pakfire_elf_check_cf_protection_aarch64(pakfire_elf* self,
                const int endianess, const uint32_t type, const char* payload) {
        int flags = 0;
 
@@ -704,13 +704,13 @@ static int pakfire_elf_check_cf_protection_aarch64(struct pakfire_elf* self,
        return flags;
 }
 
-static int pakfire_elf_check_cf_protection_riscv64(struct pakfire_elf* self,
+static int pakfire_elf_check_cf_protection_riscv64(pakfire_elf* self,
                const int endianess, const uint32_t type, const char* payload) {
        // There is nothing to do here
        return 0;
 }
 
-static int pakfire_elf_check_cf_protection_x86_64(struct pakfire_elf* self,
+static int pakfire_elf_check_cf_protection_x86_64(pakfire_elf* self,
                const int endianess, const uint32_t type, const char* payload) {
        int flags = 0;
 
@@ -736,7 +736,7 @@ static int pakfire_elf_check_cf_protection_x86_64(struct pakfire_elf* self,
        return flags;
 }
 
-static int pakfire_elf_check_cf_protection(struct pakfire_elf* self,
+static int pakfire_elf_check_cf_protection(pakfire_elf* self,
                Elf_Scn* section, const GElf_Shdr* shdr, void* data) {
        GElf_Nhdr nhdr = {};
        size_t offset = 0;
@@ -828,11 +828,11 @@ static int pakfire_elf_check_cf_protection(struct pakfire_elf* self,
        return 0;
 }
 
-int pakfire_elf_has_cf_protection(struct pakfire_elf* self) {
+int pakfire_elf_has_cf_protection(pakfire_elf* self) {
        return pakfire_elf_foreach_section(self, SHT_NOTE, pakfire_elf_check_cf_protection, NULL);
 }
 
-static int pakfire_elf_check_runpath(struct pakfire_elf* self,
+static int pakfire_elf_check_runpath(pakfire_elf* self,
                const GElf_Shdr* shdr, const GElf_Dyn* dyn, void* data) {
        const char* runpath = NULL;
        const char* value = NULL;
@@ -886,11 +886,11 @@ ERROR:
        return r;
 }
 
-int pakfire_elf_has_runpaths(struct pakfire_elf* self, char*** runpaths) {
+int pakfire_elf_has_runpaths(pakfire_elf* self, char*** runpaths) {
        return pakfire_elf_dyn_walk(self, pakfire_elf_check_runpath, runpaths);
 }
 
-static int __pakfire_elf_is_stripped(struct pakfire_elf* self,
+static int __pakfire_elf_is_stripped(pakfire_elf* self,
                Elf_Scn* section, const Elf64_Shdr* shdr, void* data) {
        // Fetch the section name
        const char* name = elf_strptr(self->elf, self->shstrndx, shdr->sh_name);
@@ -928,7 +928,7 @@ static int __pakfire_elf_is_stripped(struct pakfire_elf* self,
        return 0;
 }
 
-int pakfire_elf_is_stripped(struct pakfire_elf* self) {
+int pakfire_elf_is_stripped(pakfire_elf* self) {
        int r;
 
        switch (pakfire_elf_type(self)) {
@@ -954,7 +954,7 @@ int pakfire_elf_is_stripped(struct pakfire_elf* self) {
        return 1;
 }
 
-static int pakfire_elf_find_provides(struct pakfire_elf* self,
+static int pakfire_elf_find_provides(pakfire_elf* self,
                Elf_Scn* section, const GElf_Shdr* shdr, void* data) {
        GElf_Verdaux verdaux = {};
        GElf_Verdef verdef = {};
@@ -1012,7 +1012,7 @@ static int pakfire_elf_find_provides(struct pakfire_elf* self,
        return 0;
 }
 
-int pakfire_elf_provides(struct pakfire_elf* self, char*** provides) {
+int pakfire_elf_provides(pakfire_elf* self, char*** provides) {
        int r;
 
        // Fetch the soname
@@ -1027,7 +1027,7 @@ int pakfire_elf_provides(struct pakfire_elf* self, char*** provides) {
        return pakfire_elf_foreach_section(self, SHT_GNU_verdef, pakfire_elf_find_provides, provides);
 }
 
-static int pakfire_elf_find_requires(struct pakfire_elf* self,
+static int pakfire_elf_find_requires(pakfire_elf* self,
                Elf_Scn* section, const GElf_Shdr* shdr, void* data) {
        GElf_Vernaux vernaux = {};
        GElf_Verneed verneed = {};
@@ -1120,7 +1120,7 @@ static int pakfire_elf_find_requires(struct pakfire_elf* self,
        return 0;
 }
 
-int pakfire_elf_requires(struct pakfire_elf* self, char*** requires) {
+int pakfire_elf_requires(pakfire_elf* self, char*** requires) {
        return pakfire_elf_foreach_section(self, SHT_NULL, pakfire_elf_find_requires, requires);
 }
 
@@ -1132,7 +1132,7 @@ int pakfire_elf_requires(struct pakfire_elf* self, char*** requires) {
 #define DWARF_E_NO_DWARF 6
 #endif
 
-int pakfire_elf_foreach_source_file(struct pakfire_elf* self,
+int pakfire_elf_foreach_source_file(pakfire_elf* self,
                pakfire_elf_foreach_source_file_callback callback, void* data) {
        const char* filename = NULL;
        char basename[PATH_MAX];
index 3f9e954ab5a6f2706366fc0e946131e3488c90e2..9b0fe684cdea96f794f27e9d8ea8d4bc275ac904 100644 (file)
 #ifndef PAKFIRE_ELF_H
 #define PAKFIRE_ELF_H
 
-struct pakfire_elf;
+typedef struct pakfire_elf pakfire_elf;
 
 #include <pakfire/ctx.h>
 #include <pakfire/file.h>
 
-int pakfire_elf_open(struct pakfire_elf** elf,
+int pakfire_elf_open(pakfire_elf** elf,
        pakfire_ctx* ctx, const char* path, int fd);
-int pakfire_elf_open_file(struct pakfire_elf** elf,
+int pakfire_elf_open_file(pakfire_elf** elf,
        pakfire_ctx* ctx, struct pakfire_file* file);
 
-struct pakfire_elf* pakfire_elf_ref(struct pakfire_elf* self);
-struct pakfire_elf* pakfire_elf_unref(struct pakfire_elf* self);
+pakfire_elf* pakfire_elf_ref(pakfire_elf* self);
+pakfire_elf* pakfire_elf_unref(pakfire_elf* self);
 
-const char* pakfire_elf_path(struct pakfire_elf* self);
-int pakfire_elf_type(struct pakfire_elf* self);
-int pakfire_elf_machine(struct pakfire_elf* self);
-int pakfire_elf_is_elf64(struct pakfire_elf* self);
-int pakfire_elf_endianess(struct pakfire_elf* self);
-const char* pakfire_elf_build_id(struct pakfire_elf* self);
-const char* pakfire_elf_debuglink(struct pakfire_elf* self);
-const char* pakfire_elf_interpreter(struct pakfire_elf* elf);
-const char* pakfire_elf_soname(struct pakfire_elf* self);
+const char* pakfire_elf_path(pakfire_elf* self);
+int pakfire_elf_type(pakfire_elf* self);
+int pakfire_elf_machine(pakfire_elf* self);
+int pakfire_elf_is_elf64(pakfire_elf* self);
+int pakfire_elf_endianess(pakfire_elf* self);
+const char* pakfire_elf_build_id(pakfire_elf* self);
+const char* pakfire_elf_debuglink(pakfire_elf* self);
+const char* pakfire_elf_interpreter(pakfire_elf* elf);
+const char* pakfire_elf_soname(pakfire_elf* self);
 
-int pakfire_elf_is_pie(struct pakfire_elf* self);
-int pakfire_elf_has_ssp(struct pakfire_elf* self);
-int pakfire_elf_has_execstack(struct pakfire_elf* self);
-int pakfire_elf_is_fully_relro(struct pakfire_elf* self);
-int pakfire_elf_is_partially_relro(struct pakfire_elf* self);
-int pakfire_elf_has_cf_protection(struct pakfire_elf* self);
-int pakfire_elf_has_runpaths(struct pakfire_elf* self, char*** runpaths);
-int pakfire_elf_is_stripped(struct pakfire_elf* self);
+int pakfire_elf_is_pie(pakfire_elf* self);
+int pakfire_elf_has_ssp(pakfire_elf* self);
+int pakfire_elf_has_execstack(pakfire_elf* self);
+int pakfire_elf_is_fully_relro(pakfire_elf* self);
+int pakfire_elf_is_partially_relro(pakfire_elf* self);
+int pakfire_elf_has_cf_protection(pakfire_elf* self);
+int pakfire_elf_has_runpaths(pakfire_elf* self, char*** runpaths);
+int pakfire_elf_is_stripped(pakfire_elf* self);
 
 // Return bitmap for pakfire_elf_has_cf_protection()
 enum {
@@ -62,14 +62,14 @@ enum {
 };
 
 // Dependencies
-int pakfire_elf_provides(struct pakfire_elf* self, char*** provides);
-int pakfire_elf_requires(struct pakfire_elf* self, char*** requires);
+int pakfire_elf_provides(pakfire_elf* self, char*** provides);
+int pakfire_elf_requires(pakfire_elf* self, char*** requires);
 
 // Source Files
 typedef int (*pakfire_elf_foreach_source_file_callback)
-       (pakfire_ctx* ctx, struct pakfire_elf* elf, const char* filename, void* data);
+       (pakfire_ctx* ctx, pakfire_elf* elf, const char* filename, void* data);
 
-int pakfire_elf_foreach_source_file(struct pakfire_elf* self,
+int pakfire_elf_foreach_source_file(pakfire_elf* self,
        pakfire_elf_foreach_source_file_callback callback, void* data);
 
 #endif /* PAKFIRE_ELF_H */
index 5ac6e25b12c9b127959c217038a8c61705077871..bae7d6ddee13375b1634f242d0657b79dfdbf081 100644 (file)
@@ -1360,7 +1360,7 @@ static int pakfire_file_classify_magic(struct pakfire_file* file) {
 }
 
 static int pakfire_file_classify_elf(struct pakfire_file* file) {
-       struct pakfire_elf* elf = NULL;
+       pakfire_elf* elf = NULL;
        int r;
 
        // Don't run this if we already know that file is an ELF file
index 64a27ef0d9ae365bef6600775b6149c54c242348..ba83111a69d440ff5573276d01b18a128d5dc645 100644 (file)
@@ -44,7 +44,7 @@ struct pakfire_linter_file {
        struct pakfire_file* file;
 
        // ELF Object
-       struct pakfire_elf* elf;
+       pakfire_elf* elf;
 
        // File Descriptor
        int fd;
index 51d9ac264a5e7de2276db96c04167183363448f8..ca01b63b5717a9013a390011a965f53d93cf0fa9 100644 (file)
@@ -210,7 +210,7 @@ ERROR:
 }
 
 static int pakfire_stripper_collect_sources(
-               pakfire_ctx* ctx, struct pakfire_elf* elf, const char* filename, void* data) {
+               pakfire_ctx* ctx, pakfire_elf* elf, const char* filename, void* data) {
        struct pakfire_stripper* self = data;
        struct pakfire_file* file = NULL;
        int r;
@@ -357,7 +357,7 @@ static int __pakfire_stripper_debug_path(struct pakfire_stripper* self,
 }
 
 static int pakfire_stripper_extract_debuginfo(struct pakfire_stripper* self,
-               struct pakfire_file* file, struct pakfire_elf* elf, const char* build_id_path) {
+               struct pakfire_file* file, pakfire_elf* elf, const char* build_id_path) {
        int r;
 
        const char* path =  pakfire_file_get_abspath(file);
@@ -400,7 +400,7 @@ static int pakfire_stripper_extract_debuginfo(struct pakfire_stripper* self,
 }
 
 static int pakfire_stripper_strip_debuginfo(struct pakfire_stripper* self,
-               struct pakfire_file* file, struct pakfire_elf* elf) {
+               struct pakfire_file* file, pakfire_elf* elf) {
        char debugpath[PATH_MAX];
        char tmppath[PATH_MAX] = "";
        char** argv = NULL;
@@ -522,7 +522,7 @@ ERROR:
 static int pakfire_stripper_strip(
                pakfire_ctx* ctx, struct pakfire_file* file, void* data) {
        struct pakfire_stripper* self = data;
-       struct pakfire_elf* elf = NULL;
+       pakfire_elf* elf = NULL;
        int r;
 
        // Open the ELF file