From: Michael Tremer Date: Fri, 27 Jun 2025 10:46:12 +0000 (+0000) Subject: elf: Create its own type X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f3198070ace35d75f9c8f52261f13e7cb7fafb28;p=pakfire.git elf: Create its own type Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/build.c b/src/pakfire/build.c index 0027f2f9..98bf25de 100644 --- a/src/pakfire/build.c +++ b/src/pakfire/build.c @@ -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; diff --git a/src/pakfire/elf.c b/src/pakfire/elf.c index 97540f5f..9180c966 100644 --- a/src/pakfire/elf.c +++ b/src/pakfire/elf.c @@ -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]; diff --git a/src/pakfire/elf.h b/src/pakfire/elf.h index 3f9e954a..9b0fe684 100644 --- a/src/pakfire/elf.h +++ b/src/pakfire/elf.h @@ -21,37 +21,37 @@ #ifndef PAKFIRE_ELF_H #define PAKFIRE_ELF_H -struct pakfire_elf; +typedef struct pakfire_elf pakfire_elf; #include #include -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 */ diff --git a/src/pakfire/file.c b/src/pakfire/file.c index 5ac6e25b..bae7d6dd 100644 --- a/src/pakfire/file.c +++ b/src/pakfire/file.c @@ -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 diff --git a/src/pakfire/linter-file.c b/src/pakfire/linter-file.c index 64a27ef0..ba83111a 100644 --- a/src/pakfire/linter-file.c +++ b/src/pakfire/linter-file.c @@ -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; diff --git a/src/pakfire/stripper.c b/src/pakfire/stripper.c index 51d9ac26..ca01b63b 100644 --- a/src/pakfire/stripper.c +++ b/src/pakfire/stripper.c @@ -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