:: commit 80ad7466fd3d94a6c94e4e4e899c95dffbebd9c5

Matteo Semenzato <mattew8898@gmail.com> — 2020-03-25 22:57

parents: ee8e1b330e

Fix section loading function

diff --git a/src/lib/elf.c b/src/lib/elf.c
index 31263528..0270733b 100644
--- a/src/lib/elf.c
+++ b/src/lib/elf.c
@@ -59,19 +59,24 @@ struct elf_shdr {
     uint64_t   sh_entsize;
 };
 
-int load_section(struct echfs_file_handle *fd, void *buffer, char *name) {
+int elf_load_section(struct echfs_file_handle *fd, void *buffer, const char *name) {
     struct elf_hdr hdr;
     echfs_read(fd, &hdr, 0, sizeof(struct elf_hdr));
 
-    struct elf_shdr sections[hdr.sh_num];
-    echfs_read(fd, sections, hdr.shoff, hdr.sh_num * sizeof(struct elf_shdr));
+    struct elf_shdr shstrtab;
+    echfs_read(fd, &shstrtab, hdr.shoff + hdr.shstrndx * sizeof(struct elf_shdr),
+            sizeof(struct elf_shdr));
 
-    char names[sections[hdr.shstrndx].sh_size];
-    echfs_read(fd, names, sections[hdr.shstrndx].sh_offset, sections[hdr.shstrndx].sh_size);
+    char names[shstrtab.sh_size];
+    echfs_read(fd, names, shstrtab.sh_offset, shstrtab.sh_size);
 
     for (uint16_t i = 0; i < hdr.sh_num; i++) {
-        if(!strcmp(&names[sections[i].sh_name], name)) {
-            echfs_read(fd, buffer, sections[i].sh_offset, sections[i].sh_size);
+        struct elf_shdr section;
+        echfs_read(fd, &section, hdr.shoff + i * sizeof(struct elf_shdr),
+                   sizeof(struct elf_shdr));
+
+        if (!strcmp(&names[section.sh_name], name)) {
+            echfs_read(fd, buffer, section.sh_offset, section.sh_size);
             return 0;
         }
     }
diff --git a/src/lib/elf.h b/src/lib/elf.h
index a6f2e7c2..c8edee9e 100644
--- a/src/lib/elf.h
+++ b/src/lib/elf.h
@@ -5,6 +5,6 @@
 #include <fs/echfs.h>
 
 int elf_load(struct echfs_file_handle *fd, uint64_t *entry_point);
-void load_section(struct echfs_file_handle *fd, void *buffer, char *name);
+int elf_load_section(struct echfs_file_handle *fd, void *buffer, const char *name);
 
 #endif
tab: 248 wrap: offon