]> git.ipfire.org Git - pakfire.git/commitdiff
ELF: Add a custom pointer when walking through all sections
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Jan 2025 09:03:05 +0000 (09:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Jan 2025 09:03:05 +0000 (09:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/elf.c

index d6863d2e76890ac46a7f72c699a16b3bc00a02fa..49f9a41ebdd619531311be8312a63cfdca25d67f 100644 (file)
@@ -357,13 +357,12 @@ static int pakfire_elf_get_section(struct pakfire_elf* self,
 }
 
 typedef int (*pakfire_elf_foreach_section_callback)(struct pakfire_elf* self,
-       const Elf_Scn* section, const GElf_Shdr* shdr, Elf_Data* data);
+       Elf_Scn* section, const GElf_Shdr* shdr, void* data);
 
 static int pakfire_elf_foreach_section(struct pakfire_elf* self,
-               const Elf64_Word type, pakfire_elf_foreach_section_callback callback) {
+               const Elf64_Word type, pakfire_elf_foreach_section_callback callback, void* data) {
        Elf_Scn* section = NULL;
        GElf_Shdr shdr;
-       Elf_Data* data;
        int r = -EINVAL;
 
        // Walk through all sections
@@ -379,9 +378,6 @@ static int pakfire_elf_foreach_section(struct pakfire_elf* self,
                if (type && shdr.sh_type != type)
                        continue;
 
-               // Fetch the data
-               data = elf_getdata(section, NULL);
-
                // Call the callback
                r = callback(self, section, &shdr, data);
                if (r)
@@ -701,13 +697,16 @@ static int pakfire_elf_check_cf_protection_x86_64(struct pakfire_elf* self,
 }
 
 static int pakfire_elf_check_cf_protection(struct pakfire_elf* self,
-               const Elf_Scn* section, const GElf_Shdr* shdr, Elf_Data* data) {
+               Elf_Scn* section, const GElf_Shdr* shdr, void* data) {
        GElf_Nhdr nhdr = {};
        size_t offset = 0;
        size_t offset_name = 0;
        size_t offset_data = 0;
        int r;
 
+       // Fetch data
+       Elf_Data* d = elf_getdata(section, NULL);
+
        // Fetch the .note header
        offset = gelf_getnote(data, offset, &nhdr, &offset_name, &offset_data);
        if (!offset) {
@@ -740,7 +739,7 @@ static int pakfire_elf_check_cf_protection(struct pakfire_elf* self,
        // Fetch the endianess
        const int endianess = pakfire_elf_endianess(self);
 
-       const char* payload = (const char*)data->d_buf + offset_data;
+       const char* payload = (const char*)d->d_buf + offset_data;
 
        while (length) {
                // Read the type and size of the .note section
@@ -789,7 +788,7 @@ static int pakfire_elf_check_cf_protection(struct pakfire_elf* self,
 }
 
 int pakfire_elf_has_cf_protection(struct pakfire_elf* self) {
-       return pakfire_elf_foreach_section(self, SHT_NOTE, pakfire_elf_check_cf_protection);
+       return pakfire_elf_foreach_section(self, SHT_NOTE, pakfire_elf_check_cf_protection, NULL);
 }
 
 static int pakfire_elf_check_runpath(struct pakfire_elf* self,
@@ -851,7 +850,7 @@ int pakfire_elf_has_runpaths(struct pakfire_elf* self, char*** runpaths) {
 }
 
 static int __pakfire_elf_is_stripped(struct pakfire_elf* self,
-               const Elf_Scn* section, const Elf64_Shdr* shdr, Elf_Data* data) {
+               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);
        if (!name)
@@ -902,7 +901,7 @@ int pakfire_elf_is_stripped(struct pakfire_elf* self) {
        }
 
        // Run through all sections
-       r = pakfire_elf_foreach_section(self, SHT_NULL, __pakfire_elf_is_stripped);
+       r = pakfire_elf_foreach_section(self, SHT_NULL, __pakfire_elf_is_stripped, NULL);
        if (r < 0)
                return r;