:: commit c22492976a0d84ddb2c550169d19295068089e96

mintsuki <mintsuki@protonmail.com> — 2022-05-07 11:12

parents: 650c1443e0

elf: Do not assume relocatable unless DT_RELA present. Fixes #170

diff --git a/common/lib/elf.c b/common/lib/elf.c
index 57fe63f4..86266f99 100644
--- a/common/lib/elf.c
+++ b/common/lib/elf.c
@@ -127,8 +127,20 @@ static bool elf64_is_relocatable(uint8_t *elf, struct elf64_hdr *hdr) {
         memcpy(&phdr, elf + (hdr->phoff + i * hdr->phdr_size),
                sizeof(struct elf64_phdr));
 
-        if (phdr.p_type == PT_DYNAMIC)
-            return true;
+        if (phdr.p_type != PT_DYNAMIC) {
+            continue;
+        }
+
+        for (uint16_t j = 0; j < phdr.p_filesz / sizeof(struct elf64_dyn); j++) {
+            struct elf64_dyn dyn;
+            memcpy(&dyn, elf + (phdr.p_offset + j * sizeof(struct elf64_dyn)),
+                   sizeof(struct elf64_dyn));
+
+            switch (dyn.d_tag) {
+                case DT_RELA:
+                    return true;
+            }
+        }
     }
 
     return false;
tab: 248 wrap: offon