:: commit ee8e1b330e28f91ef172137deec702ebe0b66117

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

parents: 8494b1d880

Add function to load elf sections by name

diff --git a/src/lib/elf.c b/src/lib/elf.c
index cb81914a..31263528 100644
--- a/src/lib/elf.c
+++ b/src/lib/elf.c
@@ -13,10 +13,10 @@
 #define BITS_LE 0x01
 
 /* Indices into identification array */
-#define	EI_CLASS	4
-#define	EI_DATA		5
-#define	EI_VERSION	6
-#define	EI_OSABI	7
+#define EI_CLASS    4
+#define EI_DATA     5
+#define EI_VERSION  6
+#define EI_OSABI    7
 
 struct elf_hdr {
     uint8_t ident[16];
@@ -46,6 +46,39 @@ struct elf_phdr {
     uint64_t p_align;
 };
 
+struct elf_shdr {
+    uint32_t   sh_name;
+    uint32_t   sh_type;
+    uint64_t   sh_flags;
+    uint64_t   sh_addr;
+    uint64_t   sh_offset;
+    uint64_t   sh_size;
+    uint32_t   sh_link;
+    uint32_t   sh_info;
+    uint64_t   sh_addralign;
+    uint64_t   sh_entsize;
+};
+
+int load_section(struct echfs_file_handle *fd, void *buffer, 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));
+
+    char names[sections[hdr.shstrndx].sh_size];
+    echfs_read(fd, names, sections[hdr.shstrndx].sh_offset, sections[hdr.shstrndx].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);
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
 #define FIXED_HIGHER_HALF_OFFSET ((uint64_t)0xffffffff80000000)
 
 int elf_load(struct echfs_file_handle *fd, uint64_t *entry_point) {
diff --git a/src/lib/elf.h b/src/lib/elf.h
index 01e33edd..a6f2e7c2 100644
--- a/src/lib/elf.h
+++ b/src/lib/elf.h
@@ -5,5 +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);
 
 #endif
tab: 248 wrap: offon