:: commit 8d5f6d1504eba6a25385c751eff5ea2823a7d12b

mintsuki <mintsuki@protonmail.com> — 2024-06-08 21:37

parents: e9338aedfa

lib/elf: Panic on unresolved symbols, unless marked weak

diff --git a/common/lib/elf.c b/common/lib/elf.c
index e65f6f4c..eb24f51e 100644
--- a/common/lib/elf.c
+++ b/common/lib/elf.c
@@ -45,6 +45,8 @@
 #define BITS_LE      0x01
 #define ELFCLASS64   0x02
 #define SHT_RELA     0x00000004
+#define SHN_UNDEF    0x00000000
+#define STB_WEAK     0x00000002
 #define R_X86_64_NONE      0x00000000
 #define R_AARCH64_NONE     0x00000000
 #define R_RISCV_NONE       0x00000000
@@ -505,6 +507,13 @@ end_of_pt_segment:
 #endif
             {
                 struct elf64_sym *s = (void *)elf + symtab_offset + symtab_ent * relocation->r_symbol;
+                if (s->st_shndx == SHN_UNDEF) {
+                    if ((s->st_info >> 4) == STB_WEAK) {
+                        *ptr = 0;
+                        break;
+                    }
+                    panic(true, "elf: Unresolved symbol");
+                }
                 *ptr = slide + s->st_value
 #if defined (__aarch64__)
                        + relocation->r_addend
@@ -521,6 +530,13 @@ end_of_pt_segment:
 #endif
             {
                 struct elf64_sym *s = (void *)elf + symtab_offset + symtab_ent * relocation->r_symbol;
+                if (s->st_shndx == SHN_UNDEF) {
+                    if ((s->st_info >> 4) == STB_WEAK) {
+                        *ptr = 0;
+                        break;
+                    }
+                    panic(true, "elf: Unresolved symbol");
+                }
                 *ptr = slide + s->st_value + relocation->r_addend;
                 break;
             }
tab: 248 wrap: offon